-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchart.js
More file actions
159 lines (137 loc) · 5.01 KB
/
chart.js
File metadata and controls
159 lines (137 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var chartjs = require('chart.js')
var canvas2svg = require('canvas2svg')
//adding missing functions, see https://stackoverflow.com/questions/45563420/exporting-chart-js-charts-to-svg-using-canvas2svg-js
C2S.prototype.getContext = function (contextId) {
if (contextId == "2d" || contextId == "2D") {
return this;
}
return null;
}
C2S.prototype.style = function () {
return this.__canvas.style
}
C2S.prototype.getAttribute = function (name) {
return this[name];
}
C2S.prototype.addEventListener = function (type, listener, eventListenerOptions) { }
//bugfix, see https://github.com/gliffy/canvas2svg/issues/68
C2S.prototype.__parseFont = function () {
function parsedStyleForCSS(cssString) {
var el = document.createElement("span");
el.setAttribute("style", cssString);
return el.style; // CSSStyleDeclaration object
}
var parsed = parsedStyleForCSS('font:' + this.font);
var data = {
style: parsed['font-style'],
size: parsed['font-size'],
family: parsed['font-family'].replace(/"/g, ''),
weight: parsed['font-weight'],
decoration: parsed['text-decoration'],
href: null
};
//canvas doesn't support underline natively, but we can pass this attribute
if (this.__fontUnderline === "underline") {
data.decoration = "underline";
}
//canvas also doesn't support linking, but we can pass this as well
if (this.__fontHref) {
data.href = this.__fontHref;
}
return data;
};
//clipped groups are not rendered, so skip clipping
C2S.prototype.clip = function () { }
function mxShapeChartJsChart(bounds, fill, stroke, strokewidth) {
mxShape.call(this);
this.bounds = bounds;
this.fill = fill;
this.stroke = stroke;
this.strokewidth = (strokewidth != null) ? strokewidth : 1;
this.shadow = false;
};
/**
* Extends mxShape.
*/
mxUtils.extend(mxShapeChartJsChart, mxImageShape);
mxShapeChartJsChart.prototype.cst = {
SHAPE_CHARTJS: 'mxgraph.chartjs.abstract.chart'
};
mxShapeChartJsChart.prototype.customProperties = [
];
/**
* Function: paintVertexShape
* Untitled Diagram.drawio
* Paints the vertex shape.
*/
mxShapeChartJsChart.prototype.paintVertexShape = function (c, x, y, w, h) {
try {
var graphData = JSON.parse(this.state.cell.value);
graphData.options.devicePixelRatio = 1.0;
graphData.options.animation = false;
graphData.options.responsive = false;
var ctx = C2S(w, h)
var svg = new chartjs.Chart(ctx, graphData);
var image = 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(ctx.getSerializedSvg(true))));
c.image(x, y, w, h, image, this.preserveImageAspect, false, false);
} catch (err) {
c.setFontFamily('monospace');
c.text(x, y+h/2, 0, 0, '<pre>' + err.msg + '</pre>', mxConstants.ALIGN_LEFT, mxConstants.ALIGN_MIDDLE, false, 'html', 0, 0, 0);
c.stroke();
}
this.state.cell.valueChanged = (value) => { var lastValue = mxCell.prototype.valueChanged.call(this.state.cell, value); this.redraw(); return lastValue; }
}
mxCellRenderer.registerShape(mxShapeChartJsChart.prototype.cst.SHAPE_CHARTJS, mxShapeChartJsChart);
mxShapeChartJsChart.prototype.getConstraints = function (style, w, h) {
var constr = [];
return constr;
}
// export ui for debugging
Draw.loadPlugin(function (ui) { window.ui = ui; });
Sidebar.prototype.addChartJsPalette = function () {
var example = `
{
"type": "bar",
"data": {
"labels": ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
"datasets": [
{
"label": "# of Votes",
"data": [12, 19, 3, 5, 2, 3],
"backgroundColor": [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(255, 159, 64, 0.2)"
],
"borderColor": [
"rgba(255, 99, 132, 1)",
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
"rgba(75, 192, 192, 1)",
"rgba(153, 102, 255, 1)",
"rgba(255, 159, 64, 1)"
],
"borderWidth": 1
}]
}, "options": {
"scales": {
"yAxes": [
{
"ticks": {
"beginAtZero": true
}
}]
}
}
}`;
this.addPaletteFunctions('chartjs', 'chart.js', true, [
this.createVertexTemplateEntry('shadow=0;dashed=0;align=left;strokeWidth=1;shape=mxgraph.chartjs.abstract.chart;labelBackgroundColor=#ffffff;noLabel=1;', 200, 200, example, 'Chart', null, null, this.getTagsForStencil('mxgraph.chartjs.abstract', 'chart', 'chartjs ').join(' ')),
]);
}
Draw.loadPlugin(function (ui) {
// Adds custom sidebar entry
ui.sidebar.addChartJsPalette();
});