score:4
Accepted answer
You must not define data.labels
, because they are considered for both datasets. Instead, you should define each data point individually using an object containing x
and y
properties.
data: [
{ x: '2020-02-24', y: 900 },
{ x: '2020-03-24', y: 600 }
]
In the following runnable code snippet, I build such data
of individual datasets out of your base data array using filter()
and map()
.
const data = [
{ date: '2020-02-24', type: 'income', amount: 900 },
{ date: '2020-03-15', type: 'expense', amount: 100 },
{ date: '2020-03-20', type: 'expense', amount: 830 },
{ date: '2020-03-25', type: 'expense', amount: 50 },
{ date: '2020-03-28', type: 'expense', amount: 560 },
{ date: '2020-03-24', type: 'income', amount: 600 }
];
var worldChart = new Chart(document.getElementById('myChart'), {
type: 'line',
data: {
datasets: [
{
label: 'Expenses',
fill: false,
lineTension: 0.5,
backgroundColor: 'rgba(75,192,192,1)',
borderColor: 'rgba(0,0,0,1)',
borderWidth: 2,
data: data.filter(o => o.type == 'expense').map(o => ({ x: o.date, y: o.amount }))
},
{
label: 'Income',
fill: false,
lineTension: 0.5,
backgroundColor: 'blue',
borderColor: 'red',
borderWidth: 2,
data: data.filter(o => o.type == 'income').map(o => ({ x: o.date, y: o.amount }))
}
]
},
options: {
responsive: true,
title: {
display: false
},
scales: {
xAxes: [{
type: 'time',
time: {
unit: 'day',
tooltipFormat: 'MMM DD'
}
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="myChart" height="80"></canvas>
Source: stackoverflow.com
Related Query
- How to skip labels of a line in multiline graph in chartjs?
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to skip x Axes labels in ChartJS
- How to cut line graph in ChartJS
- chartjs 2.7 how to add labels in center of horizontal bar graph
- How to create a multiline graph with differents labels
- How to destroy line graph in ChartJS
- How do I customize y-axis labels on a Chart.js line chart?
- Chart.js how to show cursor pointer for labels & legends in line chart
- How to display Line Chart dataset point labels with Chart.js?
- ChartJS Line Graph - Multiple Lines, Show one Value on Tooltip
- How to render a vertical line on hover in chartjs
- How to align Chart.JS line chart labels to the center
- How to access labels array using chart plugin (Chart.pluginService.register) in Chartjs 2.x?
- How to remove gridlines and grid labels in Chartjs Radar?
- How to show data values or index labels in ChartJs (Latest Version)
- How to change line segment color of a line graph in Chart.js?
- How can I create a time series line graph in chart.js?
- how to change Y axis value dynamically based on user input in Chartjs for Line chart?
- How to draw Horizontal line on Bar Chart Chartjs
- How to skip labels on x-axes?
- Chartjs break line for axes tick labels text
- Chartjs line graph dataset with offset
- How to display value labels above graph bars using chart.js
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- How to dynamically set ChartJs line chart width based on dataset size?
- How do I make line charts overlay over bar charts in chartjs
- How to add ChartJS code in Html2Pdf to view image
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
More Query from same tag
- Set size chartjs in div
- ChartJS TypeError: document.getElementById(...).getContext is not a function
- Chart.js - Same min and max value of two charts
- Strikethough labels when data is hidden
- Generating different chart results on fly causes flickering
- Angular Chartjs How to make it reactive to data change?
- Using Variable in labels Chart.js of map Array
- chartjs creating equilvant background like in chartjs.org
- ChartJS, updating chart type does not update xAxis properly
- Remove redundant legends on the chart using generateLabels with ChartJS v3
- Chartjs - tooltip - different corner radius
- Chart.js passing value data dynamic
- Possible to merge 2 bars in bar chart (chart.js)
- Chartjs with Backbone
- Chartjs working with large and small values
- text inside the bar - chart.js
- How do I get a different label for each bar in a bar chart in ChartJS?
- Display maximum value of an axis
- How do I create stacked chart with Chart.js version 2.0.0-beta
- Chart js shows old data on mouse hover
- Shorten number labels in Charts.js
- chartjs stepSize by 5
- How to create chart.js vertical bar chat with rounded corners in angular 6?
- How to display pie slice data and tooltip together using chart.js
- HTML Input Form to a Javascript Variable
- My for loop does not iterate in Javascript
- Chart.js after ajax refresh there are two charts in the same container (new and old)
- ChartJS Add data from string
- Data not fully displaying on bar chart
- Chart JS not showing all data