score:0
What this code does is, it displays multi line graph using chart.js
Create a class for your labeling x and y values
//DataContract for Serializing Data - required to serve in JSON format
[DataContract]
public class LabelPoint
{
//Explicitly setting the name to be used while serializing to JSON.
[DataMember(Name = "label")]
public string Label { get; set; }
public DataPoint DataPoint { get; set; }
}
[DataContract]
public class DataPoint
{
[DataMember(Name = "x")]
public List<string> X { get; set; }
//Explicitly setting the name to be used while serializing to JSON.
[DataMember(Name = "y")]
public List<string> Y { get; set; }
}
Controller code to retrieve data
List<LabelPoint> dataPoints = GetProducts.ToList()
.GroupBy(p => p.ProductName,
(k, c) => new LabelPoint()
{
DataPoint = new DataPoint { X = c.Select(y => y.Date.ToString("dd/MM/yyyy HH:mm")).ToList(), Y = c.Select(cs => cs.Quantity).ToList() },
Label = k
}
).ToList();
ViewBag.DataPoints = dataPoints;
cshtml code to display chart and retrieve data
<canvas id="myChart"></canvas>
<script>
$(document).ready(function () {
// Get the data from the controller using viewbag
// so the data looks something like this [ { Label : "ABC" , DataPoint :[ { X: '222' , Y :60 } ] } ]
var data = @Html.Raw(Json.Encode(ViewBag.DataPoints));
// declare empty array
var dataSet = []; var qty= []; var dates= [];
// loop through the data and get the Label as well as get the created dates and qty for the array of object
for (var i = 0; i < data.length; i++) {
qty.push(data[i].DataPoint.Y);
for (var d = 0; d < data[i].DataPoint.X.length; d++) {
// we're setting this on the X- axis as the label so we need to make sure that we get all the dates between searched dates
dates.push(data[i].DataPoint.X[d]);
}
// we create an array of object, set the Lable which will display LocationName, The data here is the Quantity
dataSet.push(
{
label: data[i].Label,
data: data[i].DataPoint.Y,
fill: false,
borderColor: poolColors(qtyInLocations.length),
pointBorderColor: "black",
pointBackgroundColor: "white",
lineTension: 0.1
}
);
}
// this is the options to set the Actual label like Date And Quantity
var options = {
scales: {
xAxes: [{
scaleLabel: {
display: true,
labelString: "Date",
fontSize: 20
},
}],
yAxes: [{
ticks: {
beginAtZero:true
},
scaleLabel: {
display: true,
labelString: 'Quantity',
fontSize: 20
}
}]
}
};
// we need to remove all duplicate values from the CreatedDate array
var uniq = [ ...new Set(dates) ];
// get the canvas
var ctx = document.getElementById("myChart").getContext('2d');
// build the chart
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: uniq,
datasets:dataSet
},
options: options
});
});
/// will get get random colors each time
function dynamicColors() {
var r = Math.floor(Math.random() * 255);
var g = Math.floor(Math.random() * 255);
var b = Math.floor(Math.random() * 255);
return "rgba(" + r + "," + g + "," + b + ", 0.5)";
}
/// will display random colors each time
function poolColors(a) {
var pool = [];
for(i = 0; i < a; i++) {
pool.push(dynamicColors());
}
return pool;
}
</script>
score:14
Use scatter
type chart and showLine: true
instead of line
type with labels:
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'scatter',
data: {
datasets: [
{
label: 'Chart 1',
data: [{x: 1, y: 2}, {x: 2, y: 4}, {x: 3, y: 8},{x: 4, y: 16}],
showLine: true,
fill: false,
borderColor: 'rgba(0, 200, 0, 1)'
},
{
label: 'Chart 2',
data: [{x: 1, y: 3}, {x: 3, y: 4}, {x: 4, y: 6}, {x: 6, y: 9}],
showLine: true,
fill: false,
borderColor: 'rgba(200, 0, 0, 1)'
}
]
},
options: {
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
},
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
<canvas id="myChart"></canvas>
Source: stackoverflow.com
Related Query
- Chart.js - displaying multiple line charts using multiple labels
- Displaying line chart for multiple months using chart.js
- Displaying line chart for multiple datasets using chart.js
- Display a limited number of labels only on X-Axis of Line Chart using Chart.js
- Multiple line labels for chart js
- Displaying labels on a Doughnut Chart using Chart.js
- Why is my line chart using multiple lines to connect random points? (Chart.js)
- X and Y axis labels not displaying on line chart (Chart.js)
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- Chart.js how to show line chart without displaying the labels on xaxis and yaxis
- Building Multiple Charts Using Chart JS in an Angular Application
- Line chart plotting multiple points for duplicate data on x and y axis using chart.js
- Chart changes different on zoom when using multiple charts with chart.js and flask
- Chart.js line graph not displaying when using formatted timestamp labels
- Show multiple line graphs on the same chart using chart JS
- how to not repeat code while creating multiple charts in chart js
- Limit labels number on Chart.js line chart
- Chart Js Change Label orientation on x-Axis for Line Charts
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Moving vertical line when hovering over the chart using chart.js
- create a multi line chart using Chart.js
- How to add an on click event to my Line chart using Chart.js
- Chart.js how to show cursor pointer for labels & legends in line chart
- Display line chart with connected dots using chartJS
- How to save Chart JS charts as image without black background using blobs and filesaver?
- How to display Line Chart dataset point labels with Chart.js?
- How to add images as labels to Canvas Charts using chart.js
- Chart.js - Draw bar chart with multiple labels
- Changing x axis labels in Chart.js line chart
- Line chart with large number of labels on X axis
More Query from same tag
- Adding Multiple value in Tooltips in ChartJS charts
- Chartjs - Stacked bar chart blocking other values
- Chart.js 2.0 - How to change default appearance of canvas/chart elements
- Pie chart.js - display a no data held message
- Add MQTT topics and data to chartjs
- ChartJS, merge legends for multiple charts
- Can I bind an onclick event and edit a point in chartjs
- Chart.Js vers2 multiline to version 3
- ChartJS tooltip issue
- Getting access to already created Chart.js chart
- Date range picker and chart js
- React ChartJS 2 : Get data on clicking the chart
- Passing data to highcharts from javascript array or variable
- How to show data values or index labels in ChartJs (Latest Version)
- How to change the Y-axis value in chartjs
- Configuring locale with bundled (CDN) chartjs-adapter-date-fns.bundle.min.js?
- Multiple Graphs on one page
- Create a Graph and return it as an image on a nodejs server
- JSON data in Chart.js is splitting values
- Problem with script src for Chart.js. The CDN for Chart.js funtions fine, so my code is ok. Somehow I'm not linking the file correctly
- Ionic/Chart.js - Cannot read property 'nativeElement' of undefined
- Chart.js how to set start and end values to plot
- Angular chartjs directive
- ChartJS on small screen
- Chart.js with dual axis incorrect starting points (if negative values)
- Chartjs v2.5 - only show min and max on scale, absolute positioning on scale
- How to add chart js legends in ember?
- Chart.js - How to set animation speed?
- How do I remove CSS code arriving in my webpage?
- How i can localize days and month name in ChartJS 3.x?