forked from code4fukui/chart-bar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chart-bar.js
126 lines (114 loc) · 3.23 KB
/
chart-bar.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
import "https://cdn.plot.ly/plotly-2.15.1.min.js";
import { CSV } from "https://js.sabae.cc/CSV.js";
const defaultColors = [
"#A3BBBC",
"#98ADBF",
"#EAB9AF",
"#DD725A",
"#EEB259",
"#EEE358",
];
const colors = [...defaultColors];
for (let i = 0; i < 20; i++) {
for (let j = 0; j < defaultColors.length; j++) {
colors.push(defaultColors[j]);
}
}
class ChartBar extends HTMLElement {
constructor(data, options) {
super();
if (data !== undefined) {
const legends = [];
const dataList = [];
if (!Array.isArray(data)) {
dataList.push(Object.keys(data).map((name) => {
return { name, count: data[name] }
}));
legends.push(null);
} else {
data.forEach((d) => {
const firstKey = Object.keys(d)[0];
const baseData = (typeof(d[firstKey]) == "object") ? d[firstKey] : d;
const newData = Object.keys(baseData).map((name) => {
return { name, count: baseData[name] }
});
dataList.push(newData);
const legend = (typeof(d[firstKey]) == "object") ? firstKey : null;
legends.push(legend);
});
}
this.setData(dataList, legends, options);
} else {
const src = this.getAttribute("src");
if (src) {
this.fetchCsv(src, options);
return;
}
const txt = this.textContent.trim();
const json = CSV.toJSON(CSV.decode(txt));
this.textContent = "";
this.setData([json], [null], options);
}
}
async fetchCsv(src, options) {
const json = CSV.toJSON(await CSV.fetch(src));
this.setData([json], [null]);
}
setData(data, legends, options = {}) {
const barDatas = [];
const orientation = options["orientation"] ? options["orientation"] : "v";
data.forEach((d, index) => {
const labels = d.map((d2) => {
return d2["name"];
});
const values = d.map((d2) => {
return d2["count"];
});
const barData = {
type: "bar",
x: orientation == "v" ? labels : values,
y: orientation == "v" ? values : labels,
name: legends[index],
orientation: orientation,
marker: {
color: colors[index]
}
};
barDatas.push(barData);
});
const layout = {};
if (orientation == "v") {
layout.xaxis = {
// 日付型の場合のみこのフォーマットが適用される
tickformat: "%Y/%m/%d",
automargin: true
};
layout.yaxis = {
exponentformat: "none",
automargin: true
};
} else {
layout.yaxis = {
// 日付型の場合のみこのフォーマットが適用される
tickformat: "%Y/%m/%d",
automargin: true
};
layout.xaxis = {
exponentformat: "none",
automargin: true
};
}
if (!this.style.width) {
layout.width = !options["width"] ? 800 : options["width"];
}
if (!this.style.height) {
layout.height = !options["height"] ? 600 : options["height"];
}
if (options["legend"]) {
layout.legend = options["legend"];
}
Plotly.newPlot(this, barDatas, layout);
}
}
customElements.define("chart-bar", ChartBar);
export { ChartBar };