Telerik MVC Chart - Bind to JSON - json

How do I bind a Telerik ASP.Net MVC Chart (not the Kendo for jQuery version ) to JSON? For example, I would like to bind the following chart (Note: the series are stubbed out with dummy data for now, but hopefully you get the idea) to a JavaScript function that returns JSON. I am having trouble finding an example of how to do this with the Telerik ASP.Net MVC Chart. I do find examples with the Kendo UI for jQuery chart - but I am not using that.
#(Html.Kendo().Chart()
.Name("GallonsPerMonth")
.Title("Total Gallons Per Month")
.Legend(legend => legend
.Position(ChartLegendPosition.Top)
.Visible(true)
)
.Theme("Bootstrap")
.ChartArea(chartArea => chartArea
.Background("transparent")
.Height(600)
)
.Series(series =>
{
series.Column(new double[] { 825, 775, 875, 900, 925, 1111, 1200, 1175, 1100, 1000, 875, 800 }).Name("Estimated");
series.Line(new double[] { 700, 795, 900, 850, 950, 905, 1175, 1100, 1000, 1050, 700, 650 }).Name("Actual").Color("red");
})
.CategoryAxis(axis => axis
.Name("series-axis")
.Line(line => line.Visible(false))
)
.CategoryAxis(axis => axis
.Name("label-axis")
.Categories("Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec")
)
.ValueAxis(axis => axis//.Logarithmic()
.Numeric()
.Labels(labels => labels.Format("{0}"))
// Move the label-axis all the way down the value axis
.AxisCrossingValue(0, int.MinValue)
)
.Tooltip(tooltip => tooltip
.Visible(true)
.Format("{0}")
.Template("#= series.name #: #= value #")
)
)
dsd

You could create the chart using the MVC helper extension without the series data, so add that using JavaScript when the document is ready.
<script>
$(document).ready(function () {
$.getJSON('your-url', function (data) {
var chart = $("#GallonsPerMonth").data("kendoChart");
var series = chart.options.series;
// first series
series[0].data = data;
chart.redraw();
});
}
</script>
Note I am adding the data to the Fist Series.

Related

GoogleChart GeoChart - French region map?

I want to display a map of a region of France with GeoChart.
https://developers.google.com/chart/interactive/docs/gallery/geochart#regions-mode-format
The ISO 3166 code mentionned in the previous link is, for example, FR-U for the Provence-Alpes-Côte d'Azur region.
https://en.wikipedia.org/wiki/Provence-Alpes-C%C3%B4te_d'Azur
Here's my options:
var options = {
region: 'FR-U',
displayMode: 'markers',
colorAxis: {colors: ['green', 'blue']}
};
However this does not works. I'm getting the following error message: Requested map does not exist.. This is not due to my code since if I put FR instead of FR-U it works fine.
Is the map simply not existing?
the link you reference is the Data Format for a Regions chart
and is separate from the Configuration Options
the option for region accepts the following...
'world' - A geochart of the entire world.
A continent or a sub-continent, specified by its 3-digit code, e.g., '011' for Western Africa.
A country, specified by its ISO 3166-1 alpha-2 code, e.g., 'AU' for Australia.
A state in the United States, specified by its ISO 3166-2:US code, e.g., 'US-AL' for Alabama. Note that the resolution option must be set to either 'provinces' or 'metros'.
see following example for France...
note the resolution
google.charts.load('current', {
callback: function () {
new google.visualization.GeoChart(document.getElementById('chart_div')).draw(
google.visualization.arrayToDataTable([
['Destination', 'Popularity'],
['FR-A', 100],
['FR-B', 105],
['FR-C', 110],
['FR-P', 115],
['FR-D', 120],
['FR-E', 125],
['FR-F', 130],
['FR-G', 140],
['FR-H', 150],
['FR-I', 160],
['FR-Q', 175],
['FR-J', 190],
['FR-K', 215],
['FR-L', 235],
['FR-M', 255],
['FR-N', 280],
['FR-O', 305],
['FR-R', 335],
['FR-S', 365],
['FR-T', 400],
['FR-U', 440],
['FR-V', 480],
]),
{
colorAxis: {colors: ['green', 'blue']},
displayMode: 'regions',
region: 'FR',
resolution: 'provinces'
}
);
},
packages:['geochart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

D3js json links search using 2 attributes

so I have a page showing pathways between nodes with a json file like this:
{"nodes":[
{"name":"Node 1", "Number":"01", "x":"48.23", "y":"638.54", "Status":"starting"},
{"name":"Node 2", "Number":"02", "x":"129.05", "y":"658.49", "Status":"starting"},
{"name":"Node 3", "Number":"03", "x":"174", "y":"687.79", "Status":"starting"},
{"name":"Node 4", "Number":"04", "x":"169.96", "y":"626.92", "Status":"starting"},
{"name":"Node 5", "Number":"05", "x":"263.65", "y":"631.47", "Status":"starting"}
],
"links":[
{"source": 1, "target": 2, "value": 2},
{"source": 1, "target": 3, "value": 8},
{"source": 1, "target": 4, "value": 5},
{"source": 2, "target": 3, "value": 4},
{"source": 2, "target": 4, "value": 9}
]
}
At the moment I have it set up so that when ever I click on a node it shows all pathways to other nodes. What I want is another mode, where when I click on the node it only shows the pathway with the highest "value" attribute for that corresponding source value.
I have tried looking online for some d3js examples of something like this but haven't found anything and have no idea where to start.
atm I am using a d3.selectAll function to find all pathways for source 1 when i click on node 1 like this:
d3.selectAll(".from" + d.nodes[0].Number + ":not(.pathlabel)")
.transition()
.duration(10)
.style("stroke", "blue")
.style("display", "block")
.style("stroke-opacity", blueActive[d.nodes[0].Number])
;
My code is based on this example: http://bl.ocks.org/fhernand/9a9f93f2a6b0e83a9294
My code is on jsfiddle:
jsfiddle.net/jgs6d7fv
I just want to know how to search through my JSON file using d3.js to find a specific link based on the source and the value attributes.
I'm using the same example passed by you http://bl.ocks.org/fhernand/9a9f93f2a6b0e83a9294
This will get you all the list of link data from
d3.max(d3.selectAll(".from" + d.nodes[0].Number + ":not(.pathlabel)").data()
To get the maximum pass to a player you need to find the max pass from that is done like this
var from_max = d3.max(d3.selectAll(".from" + d.nodes[0].Number + ":not(.pathlabel)").data(), function (d) {
return (d.value);
});
To get the maximum pass to a player you need to find the max pass to that is done like this
//this will calculate the max for to
var to_max = d3.max(d3.selectAll(".to" + d.nodes[0].Number + ":not(.pathlabel)").data(), function (d) {
return (d.value);
});
Now you can display or hide the path on the basis of the max path thats done like this using the display css attribute:
d3.selectAll(".to" + d.nodes[0].Number + ":not(.pathlabel)")
.transition()
.duration(10)
.style("stroke", "orange")
.style("display", function (d) {
if (d.value == to_max) {
return "block"
} else {
return "none"
}
})
Working fiddle I have added comments here.

Google charts api - joining 3 json strings using google.visualization.data.join

I want to parse 3 separate data sets to my chart.draw() function. I've read that this is not possible so I must use the google.visualization.data.join() function to join them together. How ever I seem to be having trouble joining 3 json strings.I've tried this (as the function only takes two variables):
var joinedData1 = google.visualization.data.join(json1, json2, 'full', [[0, 0]], [1], [1]);
var joinedData2 = google.visualization.data.join(joinedData1, json3, 'full', [[0, 0]], [1], [1]);
But when I draw the chart it seems to only draw the lines of "json1" and "json3".
How can I draw the 3 lines from separate datasets?
Any help would be really appreciated.
I ran into the same issue but i'm not using JSON strings. The problem here is
google.visualization.data.join(dt1, dt2, joinMethod, keys, dt1Columns, dt2Columns);
You have mentioned only one column of dt1(joinedData1) so only the first column of the joinedData1 is joined with json3
Current:-
var joinedData1 = google.visualization.data.join(json1, json2, 'full', [[0, 0]], [1], [1]);
var joinedData2 = google.visualization.data.join(joinedData1, json3, 'full', [[0, 0]], [1], [1]);
To be changed :-
var joinedData2 = google.visualization.data.join(joinedData1, json3, 'full', [[0, 0]], **[1,2]**, [1]);

Bar chart with two series of different date/values ranges

I am using jqplot through Primefaces and Have input to Bar Chart like this:
Series 1:
label: "Company 1"
data: {"01-05-2015": 10, "06-05-2015": 3}
Series 2:
label: "Company 2"
data: {"03-05-2015": 10, "06-05-2015": 3}
When I pass this data as BarChartModel, I got data wrongly drawn on the chart.
The data follows the first series, as the Series 2 is drawn after the Series 1 dates. I've to convert the data to be as follows in order to get the chart drawn fine:
Series 1:
label: "Company 1"
data: {"01-05-2015": 10, *"03-05-2015": 0*, "06-05-2015": 3}
Series 2:
label: "Company 2"
data: { *"01-05-2015": 0* , "03-05-2015": 10, "06-05-2015": 3}
Notice the data items between * and *.
Any advice here? (if using DateAxis helps?)
I had the same problem with LinearChartModel when I has not using DateAxis.
As a workaround, I filled my series with all possible data and then reordered the list. Urg!
Should work with BarChartModel too.
Using DateAxis you just need to add your date axis with the timestamp, like this:
serie.set(new Date().getTime(), new Double(123));
or this
serie.set("2015-09-08", new Double(123));
Put the DateAxis in your LineChartModel like this:
DateAxis axis = new DateAxis("Data da inspeção");
linearModel.setZoom(true);
linearModel.getAxes().put(AxisType.X, axis);
linearModel.setExtender("linhaSetor");
And format your date in the extender.js:
function linhaSetor() {
this.cfg.axes.xaxis.tickOptions = {
show : true,
angle : 45,
formatString : '%d/%m/%y %Hh'
};
}
You don't even need to put the data in order.

How to get only unique values of a 2d array

I need to get the following 2d array from:
[[Option 10, 2.0], [Option 10, 2.0], [Option 9, 1.0], [Option 7, 1.0]]
to
[[Option 10, 2.0], [Option 9, 1.0], [Option 7, 1.0]]
I found this post (Splitting a 2D array using some() , by date, avoiding duplicates. Just want 1 unique email, not row. Where am i wrong here?) that has a very efficient way of getting unique values, but I cannot figure out how to apply it to my situation.
Your use case is simpler than the one you refer to.
try this for example :
function myFunction() {
var source = [['Option 10', 2], ['Option 10', 2], ['Option 9', 1], ['Option 7', 1]];
var dest = [];
dest.push(source[0]);
for(var n = 1 ; n< source.length ; n++){
if(dest.join().indexOf(source[n].join()) == -1){dest.push(source[n])};
}
Logger.log(dest);
}
Because 'unique' is not always simple to describe, I often use a pattern which is is, in effect, a variation of Serge's correct answer using ES5 array map/filter functions.
An edited version:
function hash(arr) {
// in this case the hash method is the same as Serge's Array.join() method,
but could be customised to suit whatever condition you need to generate
bespoke comparators such as where `1 + 3` should match `2 + 2`, or where
particular columns in the array can be omitted
return arr.join();
}
function myFunction() {
var source = [['Option 10', 2], ['Option 10', 2], ['Option 9', 1], ['Option 7', 1]];
var hash = source.map(
function (row) {
return hash(row);
}
);
source = source.filter(
function (filterRow, i) {
return hash.slice(0, i).indexOf(hash(filterRow)) < 0;
}
);
Logger.log(source);
}
I only include this as there are times when your comparison may need to flex a little. In your example this isn't important which is why Serge's is correct, but I share to show a potential expansion food for thought for when unique needs to be 'tweaked'