chartlabels-rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +29 -0
- data/Rakefile +3 -0
- data/chartlabels-rails.gemspec +30 -0
- data/lib/chartlabels.rb +9 -0
- data/lib/chartlabels/version.rb +7 -0
- data/vendor/assets/javascripts/chartjs-plugin-labels.js +490 -0
- metadata +52 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: fa8a64333f2e4d08eb1daac35fbb16ed73cb25b3398ab26c052640b442ff077c
|
|
4
|
+
data.tar.gz: 1c7a55fe488ea8dcb60e7ae619606916714fd1933592ee25f6f6a8a7a3650bd6
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: cf03052b0373c80d24bda4feafd4635068553f980be50e5924ef0bd7c0e5769f7ae817afcd20064e8a55dc6e2b762c049b9187cb1bbd51122e0b744cabb2db94
|
|
7
|
+
data.tar.gz: 33ef78ee65554c38058be9b9e3a2b1d28605abd9393ef6a3832b82026093e4cc1c91e449ba44e9744a510bb82a5fff36fb175f48ff3efde422cd4bb404649153
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 TODO: Write your name
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Chartlabels::Rails
|
|
2
|
+
|
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/chartlabels/rails`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
4
|
+
|
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'chartlabels-rails'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle install
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install chartlabels-rails
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
Visit [chartjs-plugin-label](https://github.com/emn178/chartjs-plugin-labels#usage)
|
|
26
|
+
|
|
27
|
+
## License
|
|
28
|
+
|
|
29
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/chartlabels/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "chartlabels-rails"
|
|
7
|
+
spec.version = Chartlabels::Rails::VERSION
|
|
8
|
+
spec.authors = ["jared"]
|
|
9
|
+
spec.email = ["jaredlamont90@gmail.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "chartjs plugin labels"
|
|
12
|
+
spec.homepage = "https://github.com/internetsadboy/chartlabels-rails"
|
|
13
|
+
spec.license = "MIT"
|
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
|
15
|
+
|
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
18
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
|
20
|
+
end
|
|
21
|
+
spec.bindir = "exe"
|
|
22
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
23
|
+
spec.require_paths = ["lib"]
|
|
24
|
+
|
|
25
|
+
# Uncomment to register a new dependency of your gem
|
|
26
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
|
27
|
+
|
|
28
|
+
# For more information and examples about making a new gem, checkout our
|
|
29
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
|
30
|
+
end
|
data/lib/chartlabels.rb
ADDED
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [chartjs-plugin-labels]{@link https://github.com/emn178/chartjs-plugin-labels}
|
|
3
|
+
*
|
|
4
|
+
* @version 1.1.0
|
|
5
|
+
* @author Chen, Yi-Cyuan [emn178@gmail.com]
|
|
6
|
+
* @copyright Chen, Yi-Cyuan 2017-2018
|
|
7
|
+
* @license MIT
|
|
8
|
+
*/
|
|
9
|
+
(function () {
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
if (typeof Chart === 'undefined') {
|
|
13
|
+
console.error('Can not find Chart object.');
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (typeof Object.assign != 'function') {
|
|
18
|
+
Object.assign = function (target, varArgs) {
|
|
19
|
+
if (target == null) {
|
|
20
|
+
throw new TypeError('Cannot convert undefined or null to object');
|
|
21
|
+
}
|
|
22
|
+
var to = Object(target);
|
|
23
|
+
for (var index = 1; index < arguments.length; index++) {
|
|
24
|
+
var nextSource = arguments[index];
|
|
25
|
+
if (nextSource != null) {
|
|
26
|
+
for (var nextKey in nextSource) {
|
|
27
|
+
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
|
|
28
|
+
to[nextKey] = nextSource[nextKey];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return to;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
var SUPPORTED_TYPES = {};
|
|
38
|
+
['pie', 'doughnut', 'polarArea', 'bar'].forEach(function (t) {
|
|
39
|
+
SUPPORTED_TYPES[t] = true;
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
function Label() {
|
|
43
|
+
this.renderToDataset = this.renderToDataset.bind(this);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
Label.prototype.setup = function (chart, options) {
|
|
47
|
+
this.chart = chart;
|
|
48
|
+
this.ctx = chart.ctx;
|
|
49
|
+
this.args = {};
|
|
50
|
+
this.barTotal = {};
|
|
51
|
+
var chartOptions = chart.config.options;
|
|
52
|
+
this.options = Object.assign({
|
|
53
|
+
position: 'default',
|
|
54
|
+
precision: 0,
|
|
55
|
+
fontSize: chartOptions.defaultFontSize,
|
|
56
|
+
fontColor: chartOptions.defaultFontColor,
|
|
57
|
+
fontStyle: chartOptions.defaultFontStyle,
|
|
58
|
+
fontFamily: chartOptions.defaultFontFamily,
|
|
59
|
+
shadowOffsetX: 3,
|
|
60
|
+
shadowOffsetY: 3,
|
|
61
|
+
shadowColor: 'rgba(0,0,0,0.3)',
|
|
62
|
+
shadowBlur: 6,
|
|
63
|
+
images: [],
|
|
64
|
+
outsidePadding: 2,
|
|
65
|
+
textMargin: 2,
|
|
66
|
+
overlap: true
|
|
67
|
+
}, options);
|
|
68
|
+
if (chart.config.type === 'bar') {
|
|
69
|
+
this.options.position = 'default';
|
|
70
|
+
this.options.arc = false;
|
|
71
|
+
this.options.overlap = true;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
Label.prototype.render = function () {
|
|
76
|
+
this.labelBounds = [];
|
|
77
|
+
this.chart.data.datasets.forEach(this.renderToDataset);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
Label.prototype.renderToDataset = function (dataset, index) {
|
|
81
|
+
this.totalPercentage = 0;
|
|
82
|
+
this.total = null;
|
|
83
|
+
var arg = this.args[index];
|
|
84
|
+
arg.meta.data.forEach(function (element, index) {
|
|
85
|
+
this.renderToElement(dataset, arg, element, index);
|
|
86
|
+
}.bind(this));
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
Label.prototype.renderToElement = function (dataset, arg, element, index) {
|
|
90
|
+
if (!this.shouldRenderToElement(arg.meta, element)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
this.percentage = null;
|
|
94
|
+
var label = this.getLabel(dataset, element, index);
|
|
95
|
+
if (!label) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
var ctx = this.ctx;
|
|
99
|
+
ctx.save();
|
|
100
|
+
ctx.font = Chart.helpers.fontString(this.options.fontSize, this.options.fontStyle, this.options.fontFamily);
|
|
101
|
+
var renderInfo = this.getRenderInfo(element, label);
|
|
102
|
+
if (!this.drawable(element, label, renderInfo)) {
|
|
103
|
+
ctx.restore();
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
ctx.beginPath();
|
|
107
|
+
ctx.fillStyle = this.getFontColor(dataset, element, index);
|
|
108
|
+
this.renderLabel(label, renderInfo);
|
|
109
|
+
ctx.restore();
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
Label.prototype.renderLabel = function (label, renderInfo) {
|
|
113
|
+
return this.options.arc ? this.renderArcLabel(label, renderInfo) : this.renderBaseLabel(label, renderInfo);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
Label.prototype.renderBaseLabel = function (label, position) {
|
|
117
|
+
var ctx = this.ctx;
|
|
118
|
+
if (typeof label === 'object') {
|
|
119
|
+
ctx.drawImage(label, position.x - label.width / 2, position.y - label.height / 2, label.width, label.height);
|
|
120
|
+
} else {
|
|
121
|
+
ctx.save();
|
|
122
|
+
ctx.textBaseline = 'top';
|
|
123
|
+
ctx.textAlign = 'center';
|
|
124
|
+
|
|
125
|
+
if (this.options.textShadow) {
|
|
126
|
+
ctx.shadowOffsetX = this.options.shadowOffsetX;
|
|
127
|
+
ctx.shadowOffsetY = this.options.shadowOffsetY;
|
|
128
|
+
ctx.shadowColor = this.options.shadowColor;
|
|
129
|
+
ctx.shadowBlur = this.options.shadowBlur;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
var lines = label.split('\n');
|
|
133
|
+
for (var i = 0; i < lines.length; i++) {
|
|
134
|
+
var y = position.y - this.options.fontSize / 2 * lines.length + this.options.fontSize * i;
|
|
135
|
+
ctx.fillText(lines[i], position.x, y);
|
|
136
|
+
}
|
|
137
|
+
ctx.restore();
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
Label.prototype.renderArcLabel = function (label, renderInfo) {
|
|
142
|
+
var ctx = this.ctx, radius = renderInfo.radius, view = renderInfo.view;
|
|
143
|
+
ctx.save();
|
|
144
|
+
ctx.translate(view.x, view.y);
|
|
145
|
+
if (typeof label === 'string') {
|
|
146
|
+
ctx.rotate(renderInfo.startAngle);
|
|
147
|
+
ctx.textBaseline = 'middle';
|
|
148
|
+
ctx.textAlign = 'left';
|
|
149
|
+
var lines = label.split('\n'), max = 0, widths = [], offset = 0;
|
|
150
|
+
if (this.options.position === 'border') {
|
|
151
|
+
offset = (lines.length - 1) * this.options.fontSize / 2;
|
|
152
|
+
}
|
|
153
|
+
for (var j = 0; j < lines.length; ++j) {
|
|
154
|
+
var mertrics = ctx.measureText(lines[j]);
|
|
155
|
+
if (mertrics.width > max) {
|
|
156
|
+
max = mertrics.width;
|
|
157
|
+
}
|
|
158
|
+
widths.push(mertrics.width);
|
|
159
|
+
}
|
|
160
|
+
for (var j = 0; j < lines.length; ++j) {
|
|
161
|
+
var line = lines[j];
|
|
162
|
+
var y = (lines.length - 1 - j) * -this.options.fontSize + offset;
|
|
163
|
+
ctx.save();
|
|
164
|
+
var padding = (max - widths[j]) / 2;
|
|
165
|
+
ctx.rotate(padding / radius);
|
|
166
|
+
for (var i = 0; i < line.length; i++) {
|
|
167
|
+
var char = line.charAt(i);
|
|
168
|
+
mertrics = ctx.measureText(char);
|
|
169
|
+
ctx.save();
|
|
170
|
+
ctx.translate(0, -1 * radius);
|
|
171
|
+
ctx.fillText(char, 0, y);
|
|
172
|
+
ctx.restore();
|
|
173
|
+
ctx.rotate(mertrics.width / radius);
|
|
174
|
+
}
|
|
175
|
+
ctx.restore();
|
|
176
|
+
}
|
|
177
|
+
} else {
|
|
178
|
+
ctx.rotate((view.startAngle + Math.PI / 2 + renderInfo.endAngle) / 2);
|
|
179
|
+
ctx.translate(0, -1 * radius);
|
|
180
|
+
this.renderLabel(label, { x: 0, y: 0 });
|
|
181
|
+
}
|
|
182
|
+
ctx.restore();
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
Label.prototype.shouldRenderToElement = function (meta, element) {
|
|
186
|
+
return !meta.hidden && !element.hidden && (
|
|
187
|
+
this.options.showZero ||
|
|
188
|
+
this.chart.config.type === 'polarArea' ? element._view.outerRadius !== 0 : element._view.circumference !== 0
|
|
189
|
+
);
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
Label.prototype.getLabel = function (dataset, element, index) {
|
|
193
|
+
var label;
|
|
194
|
+
if (typeof this.options.render === 'function') {
|
|
195
|
+
label = this.options.render({
|
|
196
|
+
label: this.chart.config.data.labels[index],
|
|
197
|
+
value: dataset.data[index],
|
|
198
|
+
percentage: this.getPercentage(dataset, element, index),
|
|
199
|
+
dataset: dataset,
|
|
200
|
+
index: index
|
|
201
|
+
});
|
|
202
|
+
} else {
|
|
203
|
+
switch (this.options.render) {
|
|
204
|
+
case 'value':
|
|
205
|
+
label = dataset.data[index];
|
|
206
|
+
break;
|
|
207
|
+
case 'label':
|
|
208
|
+
label = this.chart.config.data.labels[index];
|
|
209
|
+
break;
|
|
210
|
+
case 'image':
|
|
211
|
+
label = this.options.images[index] ? this.loadImage(this.options.images[index]) : '';
|
|
212
|
+
break;
|
|
213
|
+
case 'percentage':
|
|
214
|
+
default:
|
|
215
|
+
label = this.getPercentage(dataset, element, index) + '%';
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (typeof label === 'object') {
|
|
220
|
+
label = this.loadImage(label);
|
|
221
|
+
} else if (label !== null && label !== undefined) {
|
|
222
|
+
label = label.toString();
|
|
223
|
+
}
|
|
224
|
+
return label;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
Label.prototype.getFontColor = function (dataset, element, index) {
|
|
228
|
+
var fontColor = this.options.fontColor;
|
|
229
|
+
if (typeof fontColor === 'function') {
|
|
230
|
+
fontColor = fontColor({
|
|
231
|
+
label: this.chart.config.data.labels[index],
|
|
232
|
+
value: dataset.data[index],
|
|
233
|
+
percentage: this.getPercentage(dataset, element, index),
|
|
234
|
+
backgroundColor: dataset.backgroundColor[index],
|
|
235
|
+
dataset: dataset,
|
|
236
|
+
index: index
|
|
237
|
+
});
|
|
238
|
+
} else if (typeof fontColor !== 'string') {
|
|
239
|
+
fontColor = fontColor[index] || this.chart.config.options.defaultFontColor;
|
|
240
|
+
}
|
|
241
|
+
return fontColor;
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
Label.prototype.getPercentage = function (dataset, element, index) {
|
|
245
|
+
if (this.percentage !== null) {
|
|
246
|
+
return this.percentage;
|
|
247
|
+
}
|
|
248
|
+
var percentage;
|
|
249
|
+
if (this.chart.config.type === 'polarArea') {
|
|
250
|
+
if (this.total === null) {
|
|
251
|
+
this.total = 0;
|
|
252
|
+
for (var i = 0;i < dataset.data.length; ++i) {
|
|
253
|
+
this.total += dataset.data[i];
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
percentage = dataset.data[index] / this.total * 100;
|
|
257
|
+
} else if (this.chart.config.type === 'bar') {
|
|
258
|
+
if (this.barTotal[index] === undefined) {
|
|
259
|
+
this.barTotal[index] = 0;
|
|
260
|
+
for (var i = 0;i < this.chart.data.datasets.length; ++i) {
|
|
261
|
+
this.barTotal[index] += this.chart.data.datasets[i].data[index];
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
percentage = dataset.data[index] / this.barTotal[index] * 100;
|
|
265
|
+
} else {
|
|
266
|
+
percentage = element._view.circumference / this.chart.config.options.circumference * 100;
|
|
267
|
+
}
|
|
268
|
+
percentage = parseFloat(percentage.toFixed(this.options.precision));
|
|
269
|
+
if (!this.options.showActualPercentages) {
|
|
270
|
+
if (this.chart.config.type === 'bar') {
|
|
271
|
+
this.totalPercentage = this.barTotalPercentage[index] || 0;
|
|
272
|
+
}
|
|
273
|
+
this.totalPercentage += percentage;
|
|
274
|
+
if (this.totalPercentage > 100) {
|
|
275
|
+
percentage -= this.totalPercentage - 100;
|
|
276
|
+
percentage = parseFloat(percentage.toFixed(this.options.precision));
|
|
277
|
+
}
|
|
278
|
+
if (this.chart.config.type === 'bar') {
|
|
279
|
+
this.barTotalPercentage[index] = this.totalPercentage
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
this.percentage = percentage;
|
|
283
|
+
return percentage;
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
Label.prototype.getRenderInfo = function (element, label) {
|
|
287
|
+
if (this.chart.config.type === 'bar') {
|
|
288
|
+
return this.getBarRenderInfo(element, label);
|
|
289
|
+
} else {
|
|
290
|
+
return this.options.arc ? this.getArcRenderInfo(element, label) : this.getBaseRenderInfo(element, label);
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
Label.prototype.getBaseRenderInfo = function (element, label) {
|
|
295
|
+
if (this.options.position === 'outside' || this.options.position === 'border') {
|
|
296
|
+
var renderInfo, rangeFromCentre,
|
|
297
|
+
view = element._view,
|
|
298
|
+
centreAngle = view.startAngle + (view.endAngle - view.startAngle) / 2,
|
|
299
|
+
innerRadius = view.outerRadius / 2;
|
|
300
|
+
if (this.options.position === 'border') {
|
|
301
|
+
rangeFromCentre = (view.outerRadius - innerRadius) / 2 + innerRadius;
|
|
302
|
+
} else if (this.options.position === 'outside') {
|
|
303
|
+
rangeFromCentre = (view.outerRadius - innerRadius) + innerRadius + this.options.textMargin;
|
|
304
|
+
}
|
|
305
|
+
renderInfo = {
|
|
306
|
+
x: view.x + (Math.cos(centreAngle) * rangeFromCentre),
|
|
307
|
+
y: view.y + (Math.sin(centreAngle) * rangeFromCentre)
|
|
308
|
+
};
|
|
309
|
+
if (this.options.position === 'outside') {
|
|
310
|
+
var offset = this.options.textMargin + this.measureLabel(label).width / 2;
|
|
311
|
+
renderInfo.x += renderInfo.x < view.x ? -offset : offset;
|
|
312
|
+
}
|
|
313
|
+
return renderInfo;
|
|
314
|
+
} else {
|
|
315
|
+
return element.tooltipPosition();
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
Label.prototype.getArcRenderInfo = function (element, label) {
|
|
320
|
+
var radius, view = element._view;
|
|
321
|
+
if (this.options.position === 'outside') {
|
|
322
|
+
radius = view.outerRadius + this.options.fontSize + this.options.textMargin;
|
|
323
|
+
} else if (this.options.position === 'border') {
|
|
324
|
+
radius = (view.outerRadius / 2 + view.outerRadius) / 2;
|
|
325
|
+
} else {
|
|
326
|
+
radius = (view.innerRadius + view.outerRadius) / 2;
|
|
327
|
+
}
|
|
328
|
+
var startAngle = view.startAngle, endAngle = view.endAngle;
|
|
329
|
+
var totalAngle = endAngle - startAngle;
|
|
330
|
+
startAngle += Math.PI / 2;
|
|
331
|
+
endAngle += Math.PI / 2;
|
|
332
|
+
var mertrics = this.measureLabel(label);
|
|
333
|
+
startAngle += (endAngle - (mertrics.width / radius + startAngle)) / 2;
|
|
334
|
+
return {
|
|
335
|
+
radius: radius,
|
|
336
|
+
startAngle: startAngle,
|
|
337
|
+
endAngle: endAngle,
|
|
338
|
+
totalAngle: totalAngle,
|
|
339
|
+
view: view
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
Label.prototype.getBarRenderInfo = function (element, label) {
|
|
344
|
+
var renderInfo = element.tooltipPosition();
|
|
345
|
+
renderInfo.y -= this.measureLabel(label).height / 2 + this.options.textMargin;
|
|
346
|
+
return renderInfo;
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
Label.prototype.drawable = function (element, label, renderInfo) {
|
|
350
|
+
if (this.options.overlap) {
|
|
351
|
+
return true;
|
|
352
|
+
} else if (this.options.arc) {
|
|
353
|
+
return renderInfo.endAngle - renderInfo.startAngle <= renderInfo.totalAngle;
|
|
354
|
+
} else {
|
|
355
|
+
var mertrics = this.measureLabel(label),
|
|
356
|
+
left = renderInfo.x - mertrics.width / 2,
|
|
357
|
+
right = renderInfo.x + mertrics.width / 2,
|
|
358
|
+
top = renderInfo.y - mertrics.height / 2,
|
|
359
|
+
bottom = renderInfo.y + mertrics.height / 2;
|
|
360
|
+
if (this.options.renderInfo === 'outside') {
|
|
361
|
+
return this.outsideInRange(left, right, top, bottom);
|
|
362
|
+
} else {
|
|
363
|
+
return element.inRange(left, top) && element.inRange(left, bottom) &&
|
|
364
|
+
element.inRange(right, top) && element.inRange(right, bottom);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
Label.prototype.outsideInRange = function (left, right, top, bottom) {
|
|
370
|
+
var labelBounds = this.labelBounds;
|
|
371
|
+
for (var i = 0;i < labelBounds.length;++i) {
|
|
372
|
+
var bound = labelBounds[i];
|
|
373
|
+
var potins = [
|
|
374
|
+
[left, top],
|
|
375
|
+
[left, bottom],
|
|
376
|
+
[right, top],
|
|
377
|
+
[right, bottom]
|
|
378
|
+
];
|
|
379
|
+
for (var j = 0;j < potins.length;++j) {
|
|
380
|
+
var x = potins[j][0];
|
|
381
|
+
var y = potins[j][1];
|
|
382
|
+
if (x >= bound.left && x <= bound.right && y >= bound.top && y <= bound.bottom) {
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
potins = [
|
|
387
|
+
[bound.left, bound.top],
|
|
388
|
+
[bound.left, bound.bottom],
|
|
389
|
+
[bound.right, bound.top],
|
|
390
|
+
[bound.right, bound.bottom]
|
|
391
|
+
];
|
|
392
|
+
for (var j = 0;j < potins.length;++j) {
|
|
393
|
+
var x = potins[j][0];
|
|
394
|
+
var y = potins[j][1];
|
|
395
|
+
if (x >= left && x <= right && y >= top && y <= bottom) {
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
labelBounds.push({
|
|
401
|
+
left: left,
|
|
402
|
+
right: right,
|
|
403
|
+
top: top,
|
|
404
|
+
bottom: bottom
|
|
405
|
+
});
|
|
406
|
+
return true;
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
Label.prototype.measureLabel = function (label) {
|
|
410
|
+
if (typeof label === 'object') {
|
|
411
|
+
return { width: label.width, height: label.height };
|
|
412
|
+
} else {
|
|
413
|
+
var width = 0;
|
|
414
|
+
var lines = label.split('\n');
|
|
415
|
+
for (var i = 0; i < lines.length; ++i) {
|
|
416
|
+
var result = this.ctx.measureText(lines[i]);
|
|
417
|
+
if (result.width > width) {
|
|
418
|
+
width = result.width;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return { width: width, height: this.options.fontSize * lines.length };
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
Label.prototype.loadImage = function (obj) {
|
|
426
|
+
var image = new Image();
|
|
427
|
+
image.src = obj.src;
|
|
428
|
+
image.width = obj.width;
|
|
429
|
+
image.height = obj.height;
|
|
430
|
+
return image;
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
Chart.plugins.register({
|
|
434
|
+
id: 'labels',
|
|
435
|
+
beforeDatasetsUpdate: function (chart, options) {
|
|
436
|
+
if (!SUPPORTED_TYPES[chart.config.type]) {
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
if (!Array.isArray(options)) {
|
|
440
|
+
options = [options];
|
|
441
|
+
}
|
|
442
|
+
var count = options.length;
|
|
443
|
+
if (!chart._labels || count !== chart._labels.length) {
|
|
444
|
+
chart._labels = options.map(function () {
|
|
445
|
+
return new Label();
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
var someOutside = false, maxPadding = 0;
|
|
449
|
+
for (var i = 0; i < count; ++i) {
|
|
450
|
+
var label = chart._labels[i];
|
|
451
|
+
label.setup(chart, options[i]);
|
|
452
|
+
if (label.options.position === 'outside') {
|
|
453
|
+
someOutside = true;
|
|
454
|
+
var padding = label.options.fontSize * 1.5 + label.options.outsidePadding;
|
|
455
|
+
if (padding > maxPadding) {
|
|
456
|
+
maxPadding = padding;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
if (someOutside) {
|
|
461
|
+
chart.chartArea.top += maxPadding;
|
|
462
|
+
chart.chartArea.bottom -= maxPadding;
|
|
463
|
+
}
|
|
464
|
+
},
|
|
465
|
+
afterDatasetUpdate: function (chart, args, options) {
|
|
466
|
+
if (!SUPPORTED_TYPES[chart.config.type]) {
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
chart._labels.forEach(function (label) {
|
|
470
|
+
label.args[args.index] = args;
|
|
471
|
+
});
|
|
472
|
+
},
|
|
473
|
+
beforeDraw: function (chart) {
|
|
474
|
+
if (!SUPPORTED_TYPES[chart.config.type]) {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
chart._labels.forEach(function (label) {
|
|
478
|
+
label.barTotalPercentage = {};
|
|
479
|
+
});
|
|
480
|
+
},
|
|
481
|
+
afterDatasetsDraw: function (chart) {
|
|
482
|
+
if (!SUPPORTED_TYPES[chart.config.type]) {
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
chart._labels.forEach(function (label) {
|
|
486
|
+
label.render();
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
})();
|
metadata
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: chartlabels-rails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- jared
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2021-02-08 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
- jaredlamont90@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- ".gitignore"
|
|
21
|
+
- Gemfile
|
|
22
|
+
- LICENSE.txt
|
|
23
|
+
- README.md
|
|
24
|
+
- Rakefile
|
|
25
|
+
- chartlabels-rails.gemspec
|
|
26
|
+
- lib/chartlabels.rb
|
|
27
|
+
- lib/chartlabels/version.rb
|
|
28
|
+
- vendor/assets/javascripts/chartjs-plugin-labels.js
|
|
29
|
+
homepage: https://github.com/internetsadboy/chartlabels-rails
|
|
30
|
+
licenses:
|
|
31
|
+
- MIT
|
|
32
|
+
metadata: {}
|
|
33
|
+
post_install_message:
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: 2.3.0
|
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
requirements: []
|
|
48
|
+
rubygems_version: 3.2.6
|
|
49
|
+
signing_key:
|
|
50
|
+
specification_version: 4
|
|
51
|
+
summary: chartjs plugin labels
|
|
52
|
+
test_files: []
|