Passing data from db into Highcharts in Rails 3.x - javascript

I'm a rails noob, and trying to figure out how to graph some data in a database using high charts. I have the following code for the chart:
<script type="text/javascript" charset="utf-8">
$(function() {
new Highcharts.Chart({
chart: {
renderTo: "pdd_graph"
},
title: {
text: "Percent Depth Dose"
},
xAxis: {
type: "datetime"
},
yAxis: {
max: 1.4,
min: 1.2,
title: {
text: "measmt"
}
},
series: [{
pointStart: <%= 12.months.ago.to_i * 1000 %>,
data: <%= (12.months.ago.to_date..Date.today).map { |test_date| #pdd.field_5x5 } %>,
name: '5cm X 5cm Field'
}]
});
});
</script>
I want to graph the columns test_date and field_5x5 from a table called pdd . I'm just having a hard time with the proper syntax. Any help would be appreciated.
Thanks.

I came across your post while researching an issue of my own. You may need to add .inspect on the end of your data: <%= (12.months.ago.to_date..Date.today).map { |test_date| #pdd.field_5x5 } %>, line. I would like to claim credit for this wisdom but I picked it up from Ryan's Railscast on Highcharts
Another thing I did was move that logic into my controller, assign it to a var and then pass the var along... that is not going well.

Related

ApexChart: Line chart another option

Hi I am using АpexChart but I have problem setting up xaxis. The picture below is from another chart, but I'm looking for the effect it has. Note the long straight line, this means there is no data for the specific period.
How do I set up a АpexChart so I can display similar data
var options = {
series: [{
name: "Level",
data: [30,45,50,60,70,91,125]
}],
chart: {
height: 350,
type: 'line',
zoom: {
enabled: true
}
},
dataLabels: {
enabled: true
},
stroke: {
curve: 'straight'
},
title: {
text: 'Battery',
align: 'left'
},
grid: {
row: {
colors: ['#f3f3f3', 'transparent'], // takes an array which will be repeated on columns
opacity: 0.5
},
},
xaxis: {
categories: [1991,1992,1993,1994,1995,1996,1997, 1998,1999]
}
};
var chart = new ApexCharts(document.querySelector("#chart"), options);
chart.render();
The chart we see on your screenshot has been made with Highcharts, right? I know that you can do something similar with amCharts or Chart.js. But this feature is not available in ApexCharts yet. With ApexCharts, you will get blanks (see demo) if you have null values in your data.
Take a look at this issue and this pull request on GitHub.
Comment of the library author (2019), from the issue:
This has been proposed, but not worked upon yet.
Comment of the library author (2021), from the PR:
Update: This PR doesn't solve the issue the way we wanted and doesn't cover multiple use-cases.
Please bear with us. There might be a new PR with a completely different implementation.

Graphs not showing with Jinja, Flask, and CanvasJS

I am trying to get data from an API, pass it to the template with Jinja, and chart the data using CanvasJS.
When using baked in values, like in the CanvasJS examples, the graph displays properly; however, the second I try to use Jinja to input the values, the chart fails to show.
One reasons for the extra code, is that the API returns date in a "YYYY-MM-DD" form, whereas JS requires the individual values, so I split them using Jinja. Perhaps, I could split them using JS instead.
My CanvasJS code + Jinja looks like this:
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer",
{
title:{
text: "{{ ticker_symbol }}"
},
zoomEnabled: true,
axisY: {
includeZero:false,
title: "Prices",
prefix: "$ "
},
axisX: {
interval:2,
intervalType: "month",
valueFormatString: "MMM-YY",
labelAngle: -45
},
data: [
{
type: "candlestick",
dataPoints: [
{% for key, value in data.items() %}
{% set dates = key.split("-") %}
{x: new Date({{ dates[0] }}, {{ dates[1] }}, {{ dates[2] }}]),y:[{{ value['1. open'] }}, {{ value['2. high'] }}, {{ value['3. low'] }}, {{ value['4. close'] }}]},
{% endfor %}
]
}
]
});
chart.render();
}
The data is making it to the page, is displayed in the JS when viewing the source of the file in situ.
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer",
{
title:{
text: "MSFT"
},
zoomEnabled: true,
axisY: {
includeZero:false,
title: "Prices",
prefix: "$ "
},
axisX: {
interval:2,
intervalType: "month",
valueFormatString: "MMM-YY",
labelAngle: -45
},
data: [
{
type: "candlestick",
dataPoints: [
{x: new Date(2019, 11, 15]),y:[148.9300, 149.9900, 148.2700, 149.9700]},
...(repeated)...
]
}
]
});
chart.render();
}
But the graph still does not show!
I'm sure there is an issue with how the data is parsed into JS, but I am unable to spot it. One thing I noticed, is that the last date value is accompanied by an annoying "]" that does not seem to want to go away.
Any help would be greatly appreciated, thank you!
There was one major issue:
I had an extra ] when setting the x: variable for the chart. This is the primary thing that broke the entire graph.
The Jinja was fine. The JS was fine. My inability to hit {{ }} without making a square bracket was the issue.
Please commence public flogging.

Highcharts Trying to Hide/Show series

I have an Angular webapp that I am using Highcarts with to display data. The chart is configured like the following initially. In a controller (I use Coffeescript)
$scope.chartConfig = {
options:
chart:
type: 'spline'
plotBackgroundColor: null
legend:
enabled: true
title:
text: ''
subtitle:
text: ''
exporting:
url: $config.exportAddress
width: 1000
series: [{
name: ''
data: [
]
},{
name: ''
data: [
]
}]
loading: false
xAxis:
categories: [
]
title:
text: ''
yAxis:
title:
text: ''
useHighStocks: false
size:
height: 500
}
And displayed in the HTML using
<div class="panel-body" id="report-display normalchart" ng-if="!productMonthlyChart">
<highchart id="chart1" config="chartConfig"></highchart>
</div>
Using a function I push data into the chart like the following
$scope.chartConfig.series.length = 0
# Set title and chart time and and name
$scope.chartConfig.xAxis.categories = ['Build Overview']
$scope.chartConfig.options.chart.type = 'column'
$scope.graphName = 'Column Chart'
$scope.chartConfig.series.push({name: 'Admitted', data: [$scope.fullBuildTotals.admitted]})
$scope.chartConfig.series.push({name: 'Deprecated', data: [$scope.fullBuildTotals.deprecated]})
I need to be able to show and hide series on the chart however I cannot get it to work using the following show or hide code
$scope.chartConfig.series[0].show()
$scope.chartConfig.series[0].hide()
I get the following error in the console
TypeError: $scope.chartConfig.series[0].hide is not a function
at Scope.$scope.showCurrentBuildReportChartData
Does anyone have any idea why this may be happening? I also came across code where you select the DIV the Chart is contained in call a highcharts() function on it to get the chart but that did not work either.
Found a solution, not sure why series[0].show() and .hide() didn't work but an alternative is to directly do what those functions do anyway.
$scope.chartConfig.series[0].visible = true
$scope.chartConfig.series[0].visible = false
The above code works perfectly.

How to implement Highcharts column-drilldown chart in Rails application?

I'm trying to implement a Highcharts Column-Drilldown Chart in my Rails application using lazy_high_charts. I want to display data that's being pulled from my database and stored in four arrays (areas, areaScores, departments, and deptScores). I'm having trouble converting the JS from the example (JSFiddle) listed on the highchart site into ruby. I have not been able to find any resources on creating a column-drilldown chart in ruby. Any help on how to integrate the drilldown chart into my ruby application would be highly appreciated.
I have included the sample JavaScript shown on the Highcharts demo page and my controller method that populates the four arrays with data and builds the highchart.
Highcharts Column-Drilldown Chart Example (Javascript)
$(function () {
Highcharts.data({
csv: document.getElementById('tsv').innerHTML,
itemDelimiter: '\t',
parsed: function (columns) {
var brands = {},
brandsData = [],
versions = {},
drilldownSeries = [];
// Parse percentage strings
columns[1] = $.map(columns[1], function (value) {
if (value.indexOf('%') === value.length - 1) {
value = parseFloat(value);
}
return value;
});
$.each(columns[0], function (i, name) {
var brand,
version;
if (i > 0) {
// Remove special edition notes
name = name.split(' -')[0];
// Split into brand and version
version = name.match(/([0-9]+[\.0-9x]*)/);
if (version) {
version = version[0];
}
brand = name.replace(version, '');
// Create the main data
if (!brands[brand]) {
brands[brand] = columns[1][i];
} else {
brands[brand] += columns[1][i];
}
// Create the version data
if (version !== null) {
if (!versions[brand]) {
versions[brand] = [];
}
versions[brand].push(['v' + version, columns[1][i]]);
}
}
});
$.each(brands, function (name, y) {
brandsData.push({
name: name,
y: y,
drilldown: versions[name] ? name : null
});
});
$.each(versions, function (key, value) {
drilldownSeries.push({
name: key,
id: key,
data: value
});
});
// Create the chart
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Browser market shares. November, 2013'
},
subtitle: {
text: 'Click the columns to view versions. Source: netmarketshare.com.'
},
xAxis: {
type: 'category'
},
yAxis: {
title: {
text: 'Total percent market share'
}
},
legend: {
enabled: false
},
plotOptions: {
series: {
borderWidth: 0,
dataLabels: {
enabled: true,
format: '{point.y:.1f}%'
}
}
},
tooltip: {
headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
},
series: [{
name: 'Brands',
colorByPoint: true,
data: brandsData
}],
drilldown: {
series: drilldownSeries
}
})
}
});
});
My Controller:
def generateOrgBreakdownReport
# First, query the database for the data you need for the report
#jsonanswerBRKD = queryDatabaseForOrgProgressReport()
# Second, process and combine data as needed for the report
#areaBreakdown, #deptBreakdown, #employBreakdown = computeAreaAndDeptPrepareScore(#jsonanswerBRKD)
# Third, you'll need to put the processed data into a format
# Highcharts will understand for the data series it uses
# for the graph.
#THESE ARRAYS HOLD THE NAMES AND SCORES OF AREAS AND DEPARTMENTS
#deptScores, #departments, #areaScores, #areas = cycleThroughProcessedDataAndCreateHighChartsDataSetsBreakdown(#areaBreakdown, #deptBreakdown, #employBreakdown)
# Last, we put the newly made data sets for Highcharts to work its magic.
#DONT KNOW HOW TO IMPLEMENT DRILLDOWN FOR RUBY
#orgBreakdown = LazyHighCharts::HighChart.new('column') do |f|
f.chart( type: 'column' )
f.xAxis(
title: { text: "Areas" },
type: 'category'
)
f.yAxis(
title: { text: "Preparedness Score (%)"},
)
f.series(
name: "Department Score",
colorByPoint: true,
data: #deptScores
)
f.series(
name: "Area Score",
data: #areaScores
)
f.title(
text: "Organizational Breakdown"
)
f.options[:xAxis][:categories] = #areas
f.drilldown({:series=>{
name:"Dept. Score",
data: #deptScore
}
})
end
end
Thanks,
Matt
I haven't used Lazy Highcharts, but assuming it mirrors the JSON from the JavaScript API you need to add the sub-series by name, e.g.
f.series(
name: "Department Score",
colorByPoint: true,
data: #deptScores,
drilldown: "subdept" #add this
)
Then you'll need to add drilldown data, and if Lazy Highcharts supports it, it might look something like this:
f.drilldown(
series: {
id: "subdept",
data: [
["One", 1],
["Two", 2],
["Three", 3]
]
}
)
See this basic drilldown fiddle to see how the resulting Javascript should look.
To get drilldown to work in Rails you have to make sure you include the drilldown module in your JavaScript manifest file (application.js).
I also had to download the file as it was not my highcharts module catalogue. You can find the file here: http://code.highcharts.com/modules/drilldown.js
Add this to application.js:
//= require highcharts/modules/drilldown
Outside of Rails you can include the drilldown module like this:
<script src="http://code.highcharts.com/modules/drilldown.js"></script>

Highcharts not plotting Date string as Category

I am using Highcharts to plot JSON Data. The dates are in the string format.
JSON Data:
[{"BRENT_SPOT":70.88,"TRADE_DATE":"31-JUL-2009"},{"BRENT_SPOT":73.28,"TRADE_DATE":"03-AUG-2009"},{"BRENT_SPOT":74.31,"TRADE_DATE":"04-AUG-2009"},{"BRENT_SPOT":74.96,"TRADE_DATE":"05-AUG-2009"},{"BRENT_SPOT":74.4,"TRADE_DATE":"06-AUG-2009"},{"BRENT_SPOT":72.84,"TRADE_DATE":"07-AUG-2009"},{"BRENT_SPOT":73.29,"TRADE_DATE":"10-AUG-2009"},{"BRENT_SPOT":72.04,"TRADE_DATE":"11-AUG-2009"}]
HighCharts / JQuery Code :
<script>
var chart;
$(function() {
var options = {
chart: {
renderTo: 'container',
zoomType: 'xy',
type: 'line'
},
title: {
text: 'Brent Daily Price Curve (FPC as at <cfoutput>#f_date#</cfoutput>)'
},
xAxis: {
labels: {
rotation: 45,
step: 3
},
type: 'category'
},
yAxis: {
lineWidth: 1,
title: {
text: '$ USD'
},
min: 0
},
series: []
};
$.getJSON("brentpricehc_test.cfm?f_date=<cfoutput>#f_date#</cfoutput>", {}, function(jsonResult) {
var BrentUSDPrice = {
name: "Brent Spot (USD)",
type: "line",
data: [],
marker: {
radius: 2
}
};
$(jsonResult).each(function(index) {
BrentUSDPrice.data.push([this.TRADE_DATE, this.BRENT_SPOT]);
});
/*options.series[0] = BrentUSDPrice;*/
options.series.push(BrentUSDPrice);
chart = new Highcharts.Chart(options);
});
});
</script>
I'm unable to plot any values wrt each of the date strings. I tried converting the JSON dates to datetime instead but still the same issue.
Few More details (for testing purposes):
Modifying to the below line plots the graph with the correct "brent_spot" values. This means that the issue lies with the way the "trade_dates" are 'not' plotting.
BrentUSDPrice.data.push([index, this.BRENT_SPOT]);
Edit 2 : (Using Datetime type to make the code work)
JSON Data (New): Returned as TO_CHAR(TRADE_DATE, 'YYYY/MM/DD')
[{"BRENT_SPOT":70.88,"TRADE_DATE":"2009\/07\/31"},{"BRENT_SPOT":73.28,"TRADE_DATE":"2009\/08\/03"},{"BRENT_SPOT":74.31,"TRADE_DATE":"2009\/08\/04"},{"BRENT_SPOT":74.96,"TRADE_DATE":"2009\/08\/05"},{"BRENT_SPOT":74.4,"TRADE_DATE":"2009\/08\/06"},{"BRENT_SPOT":72.84,"TRADE_DATE":"2009\/08\/07"},{"BRENT_SPOT":73.29,"TRADE_DATE":"2009\/08\/10"},{"BRENT_SPOT":72.04,"TRADE_DATE":"2009\/08\/11"}]
$(jsonResult).each(function(index) {
BrentUSDPrice.data.push([new Date(this.TRADE_DATE), this.BRENT_SPOT]);
});
Server side language used : Coldfusion
Database : Oracle
Am I doing something silly somewhere?
I have just tried your code, and it works perfectly fine, see: http://jsfiddle.net/3bQne/1026/
I guess, you need to update to Highcharts 3.0.10 to get this working.
If you are using type: 'category' then you need to assign name: to the data points. See the categories entry at http://api.highcharts.com/highcharts#xAxis
If categories are present for the xAxis, names are used instead of numbers for that axis. Since Highcharts 3.0, categories can also be extracted by giving each point a name and setting axis type to "category".
So the question is whether you are using Highcharts 3.0 and if you do then it needs to look something like this:
data: [{
name: 'Point 1',
color: '#00FF00',
y: 0
}, {
name: 'Point 2',
color: '#FF00FF',
y: 5
}]
see: http://api.highcharts.com/highcharts#series.data

Categories

Resources