score:5
Accepted answer
I have solved this problem!
Solution:
Model:
public class SalesViewModel
{
public string salesdate { get; set; }
public int salesprice { get; set; }
}
Data:
public class SalesContext
{
public string ConnectionString { get; set; }
public SalesContext(string connectionString)
{
this.ConnectionString = connectionString;
}
public SalesContext()
{
}
public List<SalesViewModel> GetAllData()
{
List<SalesViewModel> list = new List<SalesViewModel>();
list.Add(new SalesViewModel() { salesdate = "1", salesprice = 3 });
list.Add(new SalesViewModel() { salesdate = "2", salesprice = 6 });
list.Add(new SalesViewModel() { salesdate = "3", salesprice = 7 });
list.Add(new SalesViewModel() { salesdate = "4", salesprice = 2 });
list.Add(new SalesViewModel() { salesdate = "5", salesprice = 1 });
return list;
}
}
Controller:
using Newtonsoft.Json;
public IActionResult Chart()
{
SalesContext sc = new SalesContext();
string json = JsonConvert.SerializeObject(sc.GetAllData());
//ViewData["chart"] = json;
ViewBag.Sales = json;
return View(sc.GetAllData());
}
View:
document.getElementById('addData').addEventListener('click', function() {
if (config.data.datasets.length > 0) {
var salesdata = @Html.Raw(ViewBag.Sales);
config.data.datasets.forEach(function(dataset) {
for (var i = 0; i < salesdata.length; i++) {
var month = MONTHS[config.data.labels.length % MONTHS.length];
config.data.labels.push(month);
dataset.data.push(salesdata[i].salesprice);
}
});
window.myLine.update();
}
});
use var salesdata = @Html.Raw(ViewBag.Sales)
to get data from controller!
use salesdata[i].salesprice
to push data to the dataset!
Thanks!
Source: stackoverflow.com
Related Query
- How to pass model object to javascript function in asp.net Core
- How pass a variable from code behind to javascript
- How to select and pass array object to data setting of chart.js config?
- Javascript function to check for missing dates on JSON object
- How to Convert an Array of Objects into an Object of Associative Arrays in Javascript
- How to write better code in es6 for formatting an object with array values
- How to send data from struts2 to a javascript function to draw a chart
- ChartJS shows chart but no data in ASP net core
- How to call a typescript arrow function and a javascript function in the same event
- How to pass a fetched object from App.js to a child component asynchronously in ReactJS v16+
- How to access the Chart object in Javascript DOM?
- How to pass variable (list) to JavaScript in Django?
- how to pass function variable from one file to another in react
- How to pass a chart.js chart data object in json from a controller in asp.net mvc
- How to pass a nested function to an HTML button and Dropdown menu
- Integrate Blazor with Chart.js: how to pass an object
- How to execute a JavaScript function with ChartJS and FreeMarker?
- How to constantly update y axis using a C# function to update a Live (Streaming) Chart.js Chart in ASP.NET core Web Application Razor Pages
- How to run Chart.js samples using source code
- How to get a value from function javascript and print it to the chart
- How to pass Arrays from backing bean to JavaScript to draw a chart using Chart.js in an Ajax call
- How to add datas to chart js from javascript array itself?
- How to convert an array of strings to float in Javascript
- Is there a JavaScript Method/ Function to loop Objects via Keys into a array?
- Populating javascript chart with Razor model data
- How to add ChartJS code in Html2Pdf to view image
- how to use javascript library in dart
- How to pass values to a chart (chart.js / morris.js)
- Why is borderColor function in chartjs running multiple times, and how to reduce it?
- How to push array multidimesional in javascript
More Query from same tag
- Chart.js v2 is there a way to draw bar chart horizontally?
- Is there a convenient way to print values to a JS chart from a CSV file without converting it?
- Chart.js zoom start at zoomed in
- connect filter to multiple charts asp.net core mvc
- How do I prevent the scale labels from being cut off in chartjs?
- Chart.js - generateLegend() call results in "undefined is not a function"
- How to shorten Chart.js label
- ChartJs in ionic Framework
- chart.js - link to other page when click on specific section in chart
- Is this graph possible using chart.js?
- How to add links to chart.js (Doughnut Charts)?
- Chart JS Line Graph multitooltipkey background color issue
- Having problems displaying multiple charts on a page
- Problems with ChartJS "Uncaught TypeError: Cannot read property 'length' of null"
- How to use the tooltipTemplate on Chart.JS 2.0
- Chart.js Bar Chart: How to remove space between the bars in v2.3?
- How to import Chart.js chartjs-plugin-datalabels npm package into an Angular 7 project
- colour sets for Chart.js border colour
- Can we draw a Line Chart with both solid and dotted line in it?
- How to add a dataset toggle to Chart.js?
- How to make dynamic chart with the next view? What libraries allows to customise exactly this like in my design?
- Multiple line types in ChartJS
- How to set ChartJS Y axis title?
- Chartkick cumulative chart by date never descending
- PrimeNg bar chart how to show a label for the y-axis
- In ChartJS how do I change the color of a label in the legend?
- show text in both y axis in dual axis chart js
- Chart.js - How to customize specific grid line depending on tick value
- Chart JS Fill Between two lines
- How can i add multiple dataset in chart.js package in Laravel