Witam wszystkich.
Walczę z chart.js od jakiegoś czasu i nie mogę sobie poradzić.
Mój kod na szybko:
<canvas id="lineChart" height="340" width="730" style="display: block; width: 730px; height: 340px;"></canvas>
<script type="application/javascript">
$(function () {
let data = {"Warszawa":{"5":69,"4":56,"3":22,"2":77,"1":50},"Krak\u00f3w":{"5":51,"4":19,"3":25,"2":70,"1":50},"Pozna\u0144":{"5":21,"2":60,"1":40}};
let obj = [];
for (let d in data) {
if (data.hasOwnProperty(d)) {
obj.push({
label: 'Ilość ogłoszeń - '+d,
backgroundColor: 'rgba(255,0,0,1.0)',
borderColor: "rgba(26,179,148,0.7)",
pointBackgroundColor: "rgba(26,179,148,1)",
pointBorderColor: "#fff",
data: data[d]
});
}
}
let lineData = {
labels: ["poniedziałek", "wtorek", "środa", "czwartek", "piątek", "sobota", "niedziela"],
datasets: []
};
let lineOptions = {
responsive: true
};
let ctx = document.getElementById("lineChart").getContext("2d");
let datas = [];
for (let i=0;i<obj.length;i++){
datas[i] = obj[i];
}
let chart = new Chart(ctx, {type: 'line', data: lineData, options:lineOptions});
chart.data.datasets = datas;
chart.update();
});
</script>
To dodawanie danych do datasets nie działa:
Uncaught TypeError: Cannot read property 'hidden' of undefined
at Chart.min.js:13
at Object.o.each (Chart.min.js:12)
at Chart.min.js:13
at Object.o.each (Chart.min.js:12)
at a.determineDataLimits (Chart.min.js:13)
at a.update (Chart.min.js:12)
at n (Chart.min.js:12)
at Object.o.each (Chart.min.js:12)
at Object.update (Chart.min.js:12)
at t.Controller.update (Chart.min.js:11)
(anonymous) @ Chart.min.js:13
o.each @ Chart.min.js:12
(anonymous) @ Chart.min.js:13
o.each @ Chart.min.js:12
determineDataLimits @ Chart.min.js:13
update @ Chart.min.js:12
n @ Chart.min.js:12
o.each @ Chart.min.js:12
update @ Chart.min.js:12
update @ Chart.min.js:11
(anonymous) @ (index):129
j @ jquery-2.1.1.js:2
fireWith @ jquery-2.1.1.js:2
ready @ jquery-2.1.1.js:2
I @ jquery-2.1.1.js:2
Chart.min.js:13 Uncaught TypeError: Cannot read property '0' of undefined
at a.draw (Chart.min.js:13)
at a.draw (Chart.min.js:11)
at t.Controller.<anonymous> (Chart.min.js:11)
at Object.o.each (Chart.min.js:12)
at t.Controller.draw (Chart.min.js:11)
at a.r.render (Chart.min.js:11)
at Object.startDigest (Chart.min.js:11)
at Chart.min.js:11
Jak dodać te dane z let data?
Mogę prosić o pomoc? Z góry dziękuje
let
nie będzie działało na wszystkich przeglądarkach. Miej to na uwadze.