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
- angular-chart.js showing black color chart in Firefox, Safari, and IE
- Update Chart in Javascript using Chart.js
- How to shade a portion of the background of ChartJS bubble chart?
- Multiple dynamic vertical lines on a chart with Chart.js
- Chart.js 2.6.0 options / scales / xAxis / gridLines doesn't work for me?
- ChartJS - Change legend to toggle buttons
- Chartjs and ChartDataLabels does not work in mdb5 pro version 2
- Two data sets with different time instances on the same ChartJs chart
- How do I create a legend for a bar chart?
- chart.js add second line in a Line chart that starts from end of first line
- Using Chart.js with Dates as x-Axis labels
- Chart JS: All Values Displayed on x-axis After Callback Conversion
- Chart.js not showing legends
- Chart.JS - Wrong Y axis
- Chart JS version 3 not showing in pdf for engine wkhtmltopdf
- Angular and CharJS - ERROR TypeError: Cannot read property '15' of undefined
- Usage of ChartJS v3 with TypeScript and Webpack
- ChartJS V3 Radar chart Label Font Size
- event click only x-axis label chartJS
- html2canvas Chart.js chart not rendered
- chart.js radar pointLabel options not working
- line-trace tooltip along x-axis?
- Trying to update data in chart.js chart
- Ng2-charts + How to customize the position of X axis labels?
- Chartjs using array in label field
- Resize Chart Independently From Legend in ChartJS for Canvas Download
- zoom and pan on charts in angular
- Trouble with chart scaling and zooming using Chart.js and chartjs-zoom-plugin
- Border radius for the bar chart in ChartJS
- Meteor / React.js / Chart.js issue