-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-ApexCharts.js
133 lines (117 loc) · 3.98 KB
/
MMM-ApexCharts.js
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
/* global Module */
/* Magic Mirror
* Module: MMM-ApexCharts
*
* By sharmstr https://github.com/sharmstr/
* MIT Licensed.
*
* v1 - Initial Release
* v2 - Better JSON support
* v2.1 - Stacked bar chart support
* v3 - Renamed option names. chartJsonFomat has reconfigured options. Added color palette support.
*
*/
Module.register("MMM-ApexCharts", {
defaults: {
chartBackground : 'transparent', //overrides gray background produced when theme mode is set to dark
chartDataLabels : true,
chartHeight : 400,
chartID : 1, //allows for multiple charts
chartJsonFormat : 'paired', //paired, labels, categories
chartJsonInterval : null,
chartJsonLabelCatName : 'labels',
chartJsonSeriesName : null,
chartJsonUrl : null,
chartMonochrome : true, //works best with default MM css
chartMonochromeColor : '#534F4F', //works best with default MM css
chartThemeMode : 'dark', //works best with default MM css
chartThemePalette : 'palette1', // Available palettes – palette1 to palette10
chartWidth : 400,
chartConfig : {}
},
requiresVersion: "2.1.0", // Required version of MagicMirror
getScripts: function() {
return ["modules/" + this.name + "/node_modules/apexcharts/dist/apexcharts.min.js"];
},
start: function() {
this.config = Object.assign({}, this.defaults, this.config);
Log.info("Starting module: " + this.name);
if (this.config.chartJsonInterval && this.config.chartJsonUrl) {
setInterval(() => {
this.updateChart();
}, this.config.chartJsonInterval);
}
},
getDom: function() {
var self = this;
self.wrapper = document.createElement("div");
self.wrapper.id = "ApexCharts_" + this.config.chartID;
self.wrapper.setAttribute("style", "position: relative; display: inline-block;");
return self.wrapper;
},
notificationReceived: function(notification, payload, sender) {
switch(notification) {
case "DOM_OBJECTS_CREATED":
this.buildChart();
break;
}
},
buildChart: function() {
this.chart = new ApexCharts(document.getElementById("ApexCharts_" + this.config.chartID), this.config.chartConfig);
this.chart.render();
this.chart.updateOptions({
chart: {
background: this.config.chartBackground,
height: this.config.chartHeight,
width: this.config.chartWidth
},
theme: {
mode: this.config.chartThemeMode,
palette: this.config.chartThemePalette,
monochrome: {
enabled: this.config.chartMonochrome,
color: this.config.chartMonochromeColor
}
},
dataLabels: {
enabled: this.config.chartDataLabels
}
})
if (this.config.chartJsonUrl) {
this.updateChart();
}
},
updateChart: function() {
Log.info("Fetching JSON: " + this.config.chartJsonUrl);
fetch(this.config.chartJsonUrl)
.then((response) => response.json())
.then((data) => {
switch(this.config.chartJsonFormat) {
case 'labels':
this.chart.updateOptions({
series: data[this.config.chartJsonSeriesName],
labels: data[this.config.chartJsonLabelCatName]
})
break;
case 'categories':
this.chart.updateOptions({
series: data[this.config.chartJsonSeriesName],
xaxis: {
categories: data[this.config.chartJsonLabelCatName]
}
})
break;
case 'paired':
default:
if(this.config.chartJsonSeriesName) {
data = data[this.config.chartJsonSeriesName]
}
this.chart.updateSeries([{
name: '',
data: data
}])
break;
}
});
}
});