Topic: trying to use one chart with multiple data sources
Drew22 free asked 4 years ago
Hi!
I would love to know if it's possible to swap between more that one js chart or swap what data source it pulls it data from via the tag
as i have 3 datatabeles thats im geting my data from each one gose in to the chart but ib been strugling on finding out how to change the data sources and mainly update the chart more dynamicly.
if someone could put a simple example code or point out what documentation i should look at? Thanks!
http://bubble.myddns.me/GroupWork-Site/appCore.php
Drew22 free answered 4 years ago
So iv finaly font time to get it working on our page end comeing up with with a pice of JavaScript to update that data sets.
`
Choose time period
Year
Month
Day
<script type="text/javascript">
//todo cahe where datas comeing from
//Supplied Datasets to display
//hourly 1 upto 24
let data1 = { "labels": $DataLabelsEncoded,"label": "Expected Usage: ", "datasets": [{ "label": "avg", "data": $dataAvgEncoded, "backgroundColor": "rgba(101, 209, 159, 0.6)", "borderColor": "rgba(101, 209, 159,1)", "borderWidth": 1 },{ "label": "Power usage", "data": $dataPointsEncoded, "backgroundColor": "rgba(93, 176, 201, 0.6)", "borderColor": "rgba(0, 10, 130, .4)", "borderWidth": 1 }] };
//days upto 31 days
let data2 = { "labels": $DataLabelsEncoded,"label": "Expected Usage:", "datasets": [{ "label": "avg", "data": $dataAvgEncoded, "backgroundColor": "rgba(101, 0, 0, 0.6)", "borderColor": "rgba(101, 209, 159,1)", "borderWidth": 1 },{ "label": "Power usage", "data": $dataPointsEncoded, "backgroundColor": "rgba(255, 255, 255, 0.6)", "borderColor": "rgba(0, 10, 130, .4)", "borderWidth": 1 }] };
//months upto 12
let data3 = { "labels": $DataLabelsEncoded,"label": "Expected Usage: ", "datasets": [{ "label": "avg", "data": $dataAvgEncoded, "backgroundColor": "rgba(101, 209, 159, 0.6)", "borderColor": "rgba(101, 209, 159,1)", "borderWidth": 1 },{ "label": "Power usage", "data": $dataPointsEncoded, "backgroundColor": "rgba(93, 176, 201, 0.6)", "borderColor": "rgba(0, 10, 130, .4)", "borderWidth": 1 }] };
// Draw the initial chart
let ctxL = $("#masterLineChart")[0].getContext('2d');
let masterLineChart = new Chart(ctxL, {
type: 'line',
data: data1,
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
// Called on Change
$(document).ready(function(){
$("select.dropdown").change(function(){
let selectedChart = $(this).children("option:selected").val();
if (selectedChart ==0){
masterLineChart["config"]["data"] = data1; //<--- THIS WORKS!
masterLineChart.update();
}
if (selectedChart ==1){
masterLineChart["config"]["data"] = data2; //<--- THIS WORKS!
masterLineChart.update();
}
if (selectedChart ==2){
masterLineChart["config"]["data"] = data3; //<--- THIS WORKS!
masterLineChart.update();
}
});
});
</script>`
if's far from perfect yet but it's functional and can now be improved on.
again thank you for trying to help me with this question.
Drew22 free answered 4 years ago
i have the chart working as intended.i want to have 3 php mysql querrys and swap between then data returned by themim assuming ill have to implment ajax to enable something like that or is there a better way to do it?
const ctxL = document.getElementById("lineChart_Year").getContext('2d');
const gradientFill = ctxL.createLinearGradient(0, 0, 0, 350);
gradientFill.addColorStop(0, "rgba(242,38,19,0.5)");
gradientFill.addColorStop(1, "rgba(0,230,64,0.5)");
let LineChartYear = new Chart(ctxL, {
type: 'line',
data: {
labels: <?php echo json_encode($dataLables, JSON_NUMERIC_CHECK); ?>,
datasets: [{
label: "Expected Usage",
data: <?php echo json_encode($dataAvg, JSON_NUMERIC_CHECK); ?>,
backgroundColor: [
'rgba(0,0,0,0)',
],
borderColor: [
'rgba(0, 10, 130, .1)',
],
borderWidth: 2
},
{
label: "Power Used",
data: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>,
backgroundColor: gradientFill,
borderColor: gradientFill,
borderWidth: 2
}
]
},
options: {
responsive: true
}
});
e
Grzegorz Bujański staff commented 4 years ago
We don't have any recommended way to implement something like this. So if it works - it's a good way.
Best, Grzegorz
Drew22 free commented 4 years ago
ok thank you for you time on this issue
glad to know im doing something out of spec for the framework keeps things intresting
Grzegorz Bujański staff commented 4 years ago
No problem. Good luck in implementing more and more interesting solutions
Best, Grzegorz.
Grzegorz Bujański staff answered 4 years ago
Hi, did you try use variables to set chart data and switch it using on click event? Something like this:
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: myData,
options: { <your options> }
});
firstDataset: { <here you first datasets> }
secondDataset: { <here you second datasets> }
$( "#firstButton" ).on( "click", () => {
myData = firstDataset;
});
$( "#secondButton" ).on( "click", () => {
myData = secondDataset;
});
Best, Grzegorz
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Resolved
- ForumUser: Free
- Premium support: No
- Technology: MDB jQuery
- MDB Version: 4.12.0
- Device: pc / mobile
- Browser: firefox/chrome
- OS: windows 10
- Provided sample code: No
- Provided link: Yes