score:3
Accepted answer
That turned out to be 'more than meets the eye'.
- Since we need all the years as x axis, we need to go through the data twice - once to get all the years, once to get the data for each year. If data is not available for that year, we have to enter
null
. Otherwise the x and y points get mismatched. - After all the years are fetched, they have to be sorted in ascending order, and we need to get the
totalnaissance
data in the same order. - You had given the
borderColor
a fixed value of#3e95cd
, but for a line chart a border color IS the line color. So I've changed that. I've also made thebackgroundColor
'transparent' andpointBackgroundColor
equal toborderColor
.
I've created a Pen.
<canvas id="myChart" width="500" height="500"></canvas>
<script>
var ctx = document.getElementById("myChart").getContext('2d');
var data = {
Nippes: [
{
annee: "1951",
totalnaissance: "1"
},
{
annee: "1986",
totalnaissance: "1"
},
{
annee: "1987",
totalnaissance: "1"
},
{
annee: "1992",
totalnaissance: "2"
}
],
"Nord-Est": [
{
annee: "1995",
totalnaissance: "1"
}
],
Ouest: [
{
annee: "1994",
totalnaissance: "2"
},
{
annee: "1995",
totalnaissance: "1"
}
],
"Sud-Est": [
{
annee: "1985",
totalnaissance: "1"
}
]
};
var departments = [];
var annees = [];
for (var department in data) {
if (data.hasOwnProperty(department)) {
var departmentData = data[department];
getYears(departmentData);
}
}
annees.sort();
for (var department in data) {
if (data.hasOwnProperty(department)) {
var departmentData = data[department];//getDataForDepartment(i);
var totalnaissanceData = getTotalNaissanceDataForDep(departmentData);
var departmentObject = prepareDepartmentDetails(department, totalnaissanceData);
departments.push(departmentObject);
}
}
var chartData = {
labels: annees,
datasets : departments
};
var chart = new Chart(ctx, {
type: "line",
data: chartData,
options: {}
});
function getDataForDepartment(index){
return data[i][Object.keys(data[i])[0]];
}
function getYears(departmentData){
for (var j = 0; j< departmentData.length; j++){
if (!annees.includes(departmentData[j].annee)){
annees.push(departmentData[j].annee);
}
}
}
function getTotalNaissanceDataForDep(departmentData){
var totalnaissanceData = [];
for (var j = 0; j < annees.length; j++){
var currentAnnee = annees[j];
var currentTotalNaissance = null;
for (var k = 0; k< departmentData.length; k++){
if (departmentData[k].annee === currentAnnee){
currentTotalNaissance = departmentData[k].totalnaissance;
break;
}
}
totalnaissanceData.push(currentTotalNaissance);
}
return totalnaissanceData;
}
function prepareDepartmentDetails(departmentName, totalnaissanceData){
var dataColor = getRandomColor();
return {
label : departmentName,
data : totalnaissanceData,
backgroundColor: 'transparent',
borderColor: dataColor,//"#3e95cd",
pointBackgroundColor : dataColor,
fill: false,
lineTension: 0,
pointRadius: 5
}
}
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
</script>
Source: stackoverflow.com
Related Query
- Display multiple datasets from array with chart.js
- Redraw Chart.js Chart with Json_encoded array from ajax request
- ChartJS update chart with multiple datasets
- ChartJs - Round borders on a doughnut chart with multiple datasets
- how to display multiple sum with chart js and laravel?
- Display two datasets from an array using chart.js in node.js
- React with chart js, labelling problems in multiple datasets for doughnut chart
- React chart2js Line chart with multiple datasets overlapping
- The chart doesn't display the data from my call to the API with Axios
- How do I display two datasets on a single chart with chartjs
- Use multiple datasets with Javascript Chart
- chart cannot read multiple datasets from the back-end (single data is fine)
- Multiple axis line chart with Chart.js and JSON data from Google Sheet
- combo bar line chart with multiple grouped/stacked datasets
- Chartjs random colors for each part of pie chart with data dynamically from database
- How to prevent first/last bars from being cut off in a chart with time scale
- Display line chart with connected dots using chartJS
- How to display Line Chart dataset point labels with Chart.js?
- Problem for display a chart with Chart.js and Angular
- How to display inline values in a stacked bar chart with Chart.js?
- How to add datas to chart js from javascript array itself?
- Chart.js - Draw bar chart with multiple labels
- ChartJS - Line Chart with different size datasets
- How to display data labels outside in pie chart with lines in ionic
- ChartJs line chart - display permanent icon above some data points with text on hover
- How can I trigger the hover mode from outside the chart with charts.js 2?
- Multiple lines / data series from JSON file with Chart.js
- Using data from API with Chart JS
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
More Query from same tag
- Chart.js - Vertical crosshair (vertical annotation that moves with mouse) in line graph
- Change the Center color of doughnut chart in Chart.Js
- How do I get the current step size of a chartjs chart whose stepSize I have not defined?
- then function not returning values Angular js
- How can make my chart.js dynamic using data from database
- Hide chart labels
- Overlay text message on top of a chart
- chartJS or/and php print_r not working together
- How to get data from JSON ReactJs
- ChartJS with AngularJS - Canvas won't display anything
- Charts.js Dynamic Ticks
- Chart.js time series showing empty plot
- How to make ng2-Chart Scrollable for large amount of data sets?
- How to create a chart with chartjs.org with data from an array?
- Unable to create Stacked Grouped Bar Chart with chart.js / react-chartjs-2 in React
- Returning fetched JSON correctly but can't get it into Chart.JS
- Multi-checkboxes And Chart Data
- How to push datasets dynamically for chart.js (bar chart)?
- How to zoom charts in chart.js using angular 7
- How can I skip data/labels in a period of time in Chartjs?
- Strange lines between horizontal bars in Chart.js 2.0
- How do I group legend entries using Chartjs?
- chart.js: Show labels outside pie chart
- Using Chart.js in functional React component, not allowing me to toggle data: "properties undefined"
- Chart.js How to sum the values in a pie chart and display them in the header
- How can I make lines fully connected in a multi-line chart even when one dataset is more sparse than another?
- My ChartJS dates don't show up correctly from coinranking API
- Chart.js - draw horizontal line in Bar Chart (type bar)
- Chartjs: how to show only max and min values on y-axis
- Chartjs columns does not appear by value