]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Title alignment options (#6908)
authorEvert Timberg <evert.timberg+github@gmail.com>
Sun, 5 Jan 2020 19:59:57 +0000 (14:59 -0500)
committerGitHub <noreply@github.com>
Sun, 5 Jan 2020 19:59:57 +0000 (14:59 -0500)
* Add alignment options for title plugin.

Alignment can be set to 'start', 'center'. or 'end'. A new sample has been added as well.

* Update sample file title

docs/configuration/title.md
samples/samples.js
samples/title/alignment.html [new file with mode: 0644]
src/plugins/plugin.title.js
test/specs/plugin.title.tests.js

index f072cb6674b40abb756c0cac75be1dd164e59667..89fca80490f5d1385eaf9bc2610d5caf352108c2 100644 (file)
@@ -7,6 +7,7 @@ The title configuration is passed into the `options.title` namespace. The global
 
 | Name | Type | Default | Description
 | ---- | ---- | ------- | -----------
+| `align` | `string` | `'center'` | Alignment of the title. [more...](#align)
 | `display` | `boolean` | `false` | Is the title shown?
 | `position` | `string` | `'top'` | Position of title. [more...](#position)
 | `fontSize` | `number` | `12` | Font size.
@@ -24,6 +25,14 @@ Possible title position values are:
 * `'bottom'`
 * `'right'`
 
+## Align
+
+Alignment of the title. Options are:
+
+* `'start'`
+* `'center'`
+* `'end'`
+
 ## Example Usage
 
 The example below would enable a title of 'Custom Chart Title' on the chart that is created.
index d95a3fe7e4fb1db39da6dd69e2d69a6e91c63e5d..c85cac37a4dd5b8e10c83b265572e80de50af9be 100644 (file)
                        title: 'Callbacks',
                        path: 'legend/callbacks.html'
                }]
+       }, {
+               title: 'Title',
+               items: [{
+                       title: 'Alignment',
+                       path: 'title/alignment.html'
+               }]
        }, {
                title: 'Tooltip',
                items: [{
diff --git a/samples/title/alignment.html b/samples/title/alignment.html
new file mode 100644 (file)
index 0000000..770db6a
--- /dev/null
@@ -0,0 +1,143 @@
+<!doctype html>
+<html>
+
+<head>
+       <title>Title Positions & Alignment</title>
+       <script src="../../dist/Chart.min.js"></script>
+       <script src="../utils.js"></script>
+       <style>
+               canvas {
+                       -moz-user-select: none;
+                       -webkit-user-select: none;
+                       -ms-user-select: none;
+               }
+               .chart-container {
+                       width: 500px;
+                       margin-left: 40px;
+                       margin-right: 40px;
+               }
+               .container {
+                       display: flex;
+                       flex-direction: row;
+                       flex-wrap: wrap;
+                       justify-content: center;
+               }
+       </style>
+</head>
+
+<body>
+       <div class="container">
+               <div class="chart-container">
+                       <canvas id="chart-title-top-start"></canvas>
+               </div>
+               <div class="chart-container">
+                       <canvas id="chart-title-top-center"></canvas>
+               </div>
+               <div class="chart-container">
+                       <canvas id="chart-title-top-end"></canvas>
+               </div>
+               <div class="chart-container">
+                       <canvas id="chart-title-left-start"></canvas>
+               </div>
+               <div class="chart-container">
+                       <canvas id="chart-title-left-center"></canvas>
+               </div>
+               <div class="chart-container">
+                       <canvas id="chart-title-left-end"></canvas>
+               </div>
+       </div>
+       <script>
+               var color = Chart.helpers.color;
+               function createConfig(titlePosition, titleAlignment, colorName) {
+                       return {
+                               type: 'line',
+                               data: {
+                                       labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
+                                       datasets: [{
+                                               label: 'My First dataset',
+                                               data: [
+                                                       randomScalingFactor(),
+                                                       randomScalingFactor(),
+                                                       randomScalingFactor(),
+                                                       randomScalingFactor(),
+                                                       randomScalingFactor(),
+                                                       randomScalingFactor(),
+                                                       randomScalingFactor()
+                                               ],
+                                               backgroundColor: color(window.chartColors[colorName]).alpha(0.5).rgbString(),
+                                               borderColor: window.chartColors[colorName],
+                                               borderWidth: 1
+                                       }]
+                               },
+                               options: {
+                                       responsive: true,
+                                       legend: {
+                                               display: false,
+                                       },
+                                       scales: {
+                                               x: {
+                                                       display: true,
+                                                       scaleLabel: {
+                                                               display: true,
+                                                               labelString: 'Month'
+                                                       }
+                                               },
+                                               y: {
+                                                       display: true,
+                                                       scaleLabel: {
+                                                               display: true,
+                                                               labelString: 'Value'
+                                                       }
+                                               }
+                                       },
+                                       title: {
+                                               align: titleAlignment,
+                                               display: true,
+                                               position: titlePosition,
+                                               text: 'Title Position: ' + titlePosition + ', Align: ' + titleAlignment
+                                       }
+                               }
+                       };
+               }
+
+               window.onload = function() {
+                       [{
+                               id: 'chart-title-top-start',
+                               titleAlignment: 'start',
+                               titlePosition: 'top',
+                               color: 'red'
+                       }, {
+                               id: 'chart-title-top-center',
+                               titleAlignment: 'center',
+                               titlePosition: 'top',
+                               color: 'orange'
+                       }, {
+                               id: 'chart-title-top-end',
+                               titleAlignment: 'end',
+                               titlePosition: 'top',
+                               color: 'yellow'
+                       }, {
+                               id: 'chart-title-left-start',
+                               titleAlignment: 'start',
+                               titlePosition: 'left',
+                               color: 'green'
+                       }, {
+                               id: 'chart-title-left-center',
+                               titleAlignment: 'center',
+                               titlePosition: 'left',
+                               color: 'blue'
+                       }, {
+                               id: 'chart-title-left-end',
+                               titleAlignment: 'end',
+                               titlePosition: 'left',
+                               color: 'purple'
+                       }].forEach(function(details) {
+                               var ctx = document.getElementById(details.id).getContext('2d');
+                               var config = createConfig(details.titlePosition, details.titleAlignment, details.color);
+                               new Chart(ctx, config);
+                       });
+               };
+       </script>
+</body>
+
+</html>
index 3e2fa79efa087ab0914826d0630ba33569fdef84..f81bd91c19034cd60347f5a842950f6dc9170327 100644 (file)
@@ -6,6 +6,7 @@ const helpers = require('../helpers/index');
 const layouts = require('../core/core.layouts');
 
 defaults._set('title', {
+       align: 'center',
        display: false,
        fontStyle: 'bold',
        fullWidth: true,
@@ -120,35 +121,64 @@ class Title extends Element {
 
        // Actually draw the title block on the canvas
        draw() {
-               var me = this;
-               var ctx = me.ctx;
-               var opts = me.options;
+               const me = this;
+               const ctx = me.ctx;
+               const opts = me.options;
 
                if (!opts.display) {
                        return;
                }
 
-               var fontOpts = helpers.options._parseFont(opts);
-               var lineHeight = fontOpts.lineHeight;
-               var offset = lineHeight / 2 + me._padding.top;
-               var rotation = 0;
-               var top = me.top;
-               var left = me.left;
-               var bottom = me.bottom;
-               var right = me.right;
-               var maxWidth, titleX, titleY;
+               const fontOpts = helpers.options._parseFont(opts);
+               const lineHeight = fontOpts.lineHeight;
+               const offset = lineHeight / 2 + me._padding.top;
+               let rotation = 0;
+               const top = me.top;
+               const left = me.left;
+               const bottom = me.bottom;
+               const right = me.right;
+               let maxWidth, titleX, titleY;
+               let align;
 
                ctx.fillStyle = helpers.valueOrDefault(opts.fontColor, defaults.fontColor); // render in correct colour
                ctx.font = fontOpts.string;
 
                // Horizontal
                if (me.isHorizontal()) {
-                       titleX = left + ((right - left) / 2); // midpoint of the width
+                       switch (opts.align) {
+                       case 'start':
+                               titleX = left;
+                               align = 'left';
+                               break;
+                       case 'end':
+                               titleX = right;
+                               align = 'right';
+                               break;
+                       default:
+                               titleX = left + ((right - left) / 2);
+                               align = 'center';
+                               break;
+                       }
+
                        titleY = top + offset;
                        maxWidth = right - left;
                } else {
                        titleX = opts.position === 'left' ? left + offset : right - offset;
-                       titleY = top + ((bottom - top) / 2);
+
+                       switch (opts.align) {
+                       case 'start':
+                               titleY = opts.position === 'left' ? bottom : top;
+                               align = 'left';
+                               break;
+                       case 'end':
+                               titleY = opts.position === 'left' ? top : bottom;
+                               align = 'right';
+                               break;
+                       default:
+                               titleY = top + ((bottom - top) / 2);
+                               align = 'center';
+                               break;
+                       }
                        maxWidth = bottom - top;
                        rotation = Math.PI * (opts.position === 'left' ? -0.5 : 0.5);
                }
@@ -156,7 +186,7 @@ class Title extends Element {
                ctx.save();
                ctx.translate(titleX, titleY);
                ctx.rotate(rotation);
-               ctx.textAlign = 'center';
+               ctx.textAlign = align;
                ctx.textBaseline = 'middle';
 
                var text = opts.text;
index 086add5583ada5352affc6acb69dc4a531d4d9e1..484411356a65dfd94605b2952073f959ddc985b1 100644 (file)
@@ -5,6 +5,7 @@ var Title = Chart.plugins.getAll().find(p => p.id === 'title')._element;
 describe('Title block tests', function() {
        it('Should have the correct default config', function() {
                expect(Chart.defaults.title).toEqual({
+                       align: 'center',
                        display: false,
                        position: 'top',
                        fullWidth: true,