score:1
Accepted answer
You need two arrays for creating your chart. One of them indicates titles and another one shows the number of each titles. You have titles in the client side, so you only need the number of each options and it could be fetched from a simple server method like:
[HttpGet]
public JsonResult Chart()
{
var data = new int[] { 4, 2, 5 }; // fill it up whatever you want, but the number of items should be equal with your options
return JsonConvert.SerializeObject(data)
}
The client side code is here:
var aLabels = ["Agree","Somewhat Agree","Disagree"];
var aDatasets1 = [4,2,5]; //fetch these from the server
var dataT = {
labels: aLabels,
datasets: [{
label: "Test Data",
data: aDatasets1,
fill: false,
backgroundColor: ["rgba(54, 162, 235, 0.2)", "rgba(255, 99, 132, 0.2)", "rgba(255, 159, 64, 0.2)", "rgba(255, 205, 86, 0.2)", "rgba(75, 192, 192, 0.2)", "rgba(153, 102, 255, 0.2)", "rgba(201, 203, 207, 0.2)"],
borderColor: ["rgb(54, 162, 235)", "rgb(255, 99, 132)", "rgb(255, 159, 64)", "rgb(255, 205, 86)", "rgb(75, 192, 192)", "rgb(153, 102, 255)", "rgb(201, 203, 207)"],
borderWidth: 1
}]
};
var opt = {
responsive: true,
title: { display: true, text: 'TEST CHART' },
legend: { position: 'bottom' },
//scales: {
// xAxes: [{ gridLines: { display: false }, display: true, scaleLabel: { display: false, labelString: '' } }],
// yAxes: [{ gridLines: { display: false }, display: true, scaleLabel: { display: false, labelString: '' }, ticks: { stepSize: 50, beginAtZero: true } }]
//}
};
var ctx = document.getElementById("myChart").getContext("2d");
var myNewChart = new Chart(ctx, {
type: 'pie',
data: dataT,
options: opt
});
<script src="https://github.com/chartjs/Chart.js/releases/download/v2.7.1/Chart.min.js"></script>
<div Style="font-family: Corbel; font-size: small ;text-align:center " class="row">
<div style="width:100%;height:100%">
<canvas id="myChart" style="padding: 0;margin: auto;display: block; "> </canvas>
</div>
</div>
score:0
If you are still looking to use json for chart.js charts.
Here is a solution which fetch a json file and render it on chart.js chart.
fetch('https://s3-us-west-2.amazonaws.com/s.cdpn.io/827672/CSVtoJSON.json')
.then(function(response) {
return response.json();
})
.then(function(ids) {
new Chart(document.getElementById("bar-chart"), {
type: 'bar',
data: {
labels: ids.map(function(id) {
return id.Label;
}),
datasets: [
{
label: "value2",
backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
data: ids.map(function(id) {
return id.Value2;
}),
},
{
label: "value",
//backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
data: ids.map(function(id) {
return id.Value;
}),
},
]
},
options: {
legend: { display: false },
title: {
display: true,
text: 'Sample Json Data Chart'
}
}
});
});
Source: stackoverflow.com
Related Query
- How to use JSON data in creating a chart with chartjs?
- How to display chart using Chartjs with JSON data in Laravel
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to create chartjs chart with data from database C#
- How to create a custom tooltip for chartJS graph with data in JSON array in JavaScript?
- How to reuse a Chartjs Chart component in with different Data and get past the **Canvas is already in use** error?
- How to use JSON data as chartjs data?
- Chart.js - creating time series freqency chart with JSON data
- How to use computed property with data from JSON in data object for time axis in Vue using Chart.js
- Use multi data to draw chart with chartjs
- Chartjs random colors for each part of pie chart with data dynamically from database
- ChartJS - Draw chart with label by month, data by day
- How to fix chart Legends width-height with overflow scroll in ChartJS
- How to display data labels outside in pie chart with lines in ionic
- Chartjs - data format for bar chart with multi-level x-axes
- ChartJs line chart - display permanent icon above some data points with text on hover
- How to use 'time' (data from database, data type: timestamp ) for plotting graph in Chart JS
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- Use transparent stroke color with chartjs pie chart
- Angular chart how to show the legend data value by default along with legend name
- How to use an Addon with ChartJS on Angular 4
- How do I use my chart.js line chart with handlebars?
- How to get chart from data points (arrays) with inconsistent time intervals and chart.js?
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- how to display chart data as html table chartjs
- How can I load multiple Chartjs charts with different data on the same page?
- How to use async data for use with chart.js in ionic
- How To Use Api Data With Vue Chart.js
- How can I show JSON data in Chart.js with Javascript if static values are working but dynamic from mySQL are not?
- How do I display chart on my HTML page based on JSON data sent by Node JS
More Query from same tag
- Chart.js tooltip background color setting
- ChartJS 2.6 - Change color of bar in Bar chart programmatically
- Chart.js v3.60 - Add % symbol to the end of the tooltip label on a doughnut chart?
- How can I get access to the index of the highlighted dataset in Chart.js
- Cannot read property 'transition' null chartjs
- chartjs-plugin-zoom: get currently visible values when the page first loads
- How to fill a graph by a color till a vertical line using chart.js
- chartJSRadar downloadhandler creating empty png
- chart.js Multiple barchart in time scale won't display all
- How to style a pie chart in chart js? I want to change the border color, border width and give them shadow
- How can I create a long tail chart with chart.js
- Convert/transpose one array into another in JavaScript?
- How to remove percentage calculation on top of bar in ng2-chart bar chart
- How can I add a unit to the end of my Y Axis values in ChartJS?
- How to know the length of the horizontal bar in Chart.js
- Updating Chartjs dataset in Ionic
- Chart.js with Django - Using REST
- Chart.js Clipping on Top of Line Graph
- Canvas Emptied but Reappears
- How do I import Chart.js Helper Functions in Django?
- Property 'elements' does not exist on type 'typeof Chart'
- Chart JS - Title missing when clearing canvas if no data is available
- Undefined values in chart.js from json
- Change tool-tip direction in react-chartjs2
- Is it possible to create different stepsize for different yAxis intervals on chartJs?
- Dynamically populating data into chart in Chart.js
- Creating charts using chart.js
- Chart.js not being displayed inside Jquery Dialog
- Display Bar chart values in the graph - ChartJS
- Chart.Mvc colliding with System.Web.Helpers.Chart