Use GoogleCharts change colors BarChart ruby - ruby

I'm trying to use GoogleChart to generate Pie and Bar charts from the same options (With custom if needed)
Everything works fine on the Pie, but the Bar charts don't want to take the colors. So I did research and I'm trying to add some series to force it to change the colors. But, so far, still blue!
Do you have any idea?
option = {
width: 700,
height: 200,
colors: #colors,#basic array of colors
# Don't display negative values
min_value: 0,
title: vote.question,
backgroundColor: { fill:'transparent' },
is3D: true,
# Displays items even if the value is 0
sliceVisibilityThreshold: 0,
legend: {alignment: "center"},
vAxis: {format:'#%'},
series: [{colors: '#3366cc'}, {colors: '#dc3912'}, {colors: '#ff9900'}, {colors: '#dc3912'}],
isStacked: true
}
I tried to add some series 'in hard' but...

BarCharts color data by series, and it sounds like you have a single data series with multiple points. If you want to change the bars' colors, you need to add a "style" role column to your DataTable, and specify the colors in that:
var data = new google.visualization.DataTable();
data.addColumn('string', 'Category');
data.addColumn('number', 'Value');
data.addColumn({type: 'string', role: 'style'});
data.addRows([
['Foo', 5, 'color: #3366cc'],
['Bar', 4, 'color: #dc3912'],
['Baz', 7, 'color: #ff9900'],
['Cad', 3, 'color: #dc3912']
]);

My solution, using #asgallant tips. In Ruby.
#colors are basically an array of predefined colors.
#colors = ['#3366cc','#dc3912',"#ff9900","#109618","#990099","#0099c6","#dd4477","#66aa00","#b82e2e","#316395","#994499","#22aa99","#aaaa11","#6633cc","#e67300","#8b0707","#651067","#329262","#5574a6","#3b3eac","#b77322","#16d620","#b91383","#f4359e","#9c5935","#a9c413","#2a778d","#668d1c","#bea413","#0c5922","#743411"]
# Initialize the datatable that will contains our charts data.
data_table = GoogleVisualr::DataTable.new
data_titles = []
data_votes = []
# Headers
data_table.new_column('string', t('choices'))
data_table.new_column('number', t('shared.new_top_bar.votes'))
data_table.new_column('string', 'color', nil, 'style');
vote.vote_options.each do |vote_option|
data_titles.push(escape_javascript(vote_option.title))
data_votes.push(vote_option.votes)
end
data_titles.each_with_index do |elm, i|
data_table.add_row([data_titles[i], data_votes[i], "color: "+#colors[i]])
end
# Configure the default options for the charts.
option = {
width: 700,
height: 200,
#colors: #colors,
# Don't display negative values
min_value: 0,
title: vote.question,
backgroundColor: { fill:'transparent' },
is3D: true,
# Displays items even if the value is 0
sliceVisibilityThreshold: 0,
legend: {alignment: 'center'}
}
if data_titles.size > 5
# Customisation if too much data.
option[:height] = data_titles.size * 50# Make it bigger if there is too much parts to draw.
end
#charts[vote] = GoogleVisualr::Interactive::PieChart.new(data_table, option)
#charts2[vote] = GoogleVisualr::Interactive::BarChart.new(data_table, option)

Related

AmChart not appearing in md-dialog

I have an AmChart that I want to appear in an md-dialog. It's passed a JSON dataProvider and yet nothing appears.
dialog.tmpl.html:
<md-dialog aria-label="Project Zone Chart">
<md-toolbar>
<div class="md-toolbar-tools">
<h2>Project Zone Chart</h2>
<span flex></span>
<md-button class="md-icon-button" ng-click="closeDialog()">
<md-icon aria-label="Close dialog">close</md-icon>
</md-button>
</div>
</md-toolbar>
<md-dialog-content>
<p><div id="projZoneChart" style="width: 100%; height: auto;"></div></p>
</md-dialog-content>
</md-dialog>
controller.js:
var projZoneChartOps = {
type: "serial",
valueAxes: [{
minorGridAlpha: 0.08,
minorGridEnabled: true,
position: "top",
gridAlpha: 0,
precision: 0
}],
startDuration: 1,
graphs: [{
type: "column",
fillAlphas: 1,
lineAlpha: 0,
valueField: "value",
colorField: "color",
lineAlpha: 0
}],
rotate: true,
categoryField: "metric",
categoryAxis: {
gridPosition: "start",
parseDates: false,
gridAlpha: 0
}
};
createChart($scope.chartdata, projZoneChartOps);
function createChart(chartData, chartOps){
$scope.projZoneChart = AmCharts.makeChart("projZoneChart", chartOps);
$scope.projZoneChart.dataProvider = chartData;
However nothing appears in the dialog at all. Is there a problem with my chartOps?
Note: the chartData variable is a JSON object with two fields, startOfWeek (supposed to be the x-axis) and metric (supposed to be the y-axis)
There are a few issues/comments:
1) Why aren't you assigning the data to the chartOps object before calling makeChart?
chartOps.dataProvider = chartData;
$scope.projZoneChart = AmCharts.makeChart("projZoneChart", chartOps);
This will actually make the chart start off with your data. The way you're doing it will require that you call validateData() on your chart object after manually setting the dataProvider, which is unnecessary overhead compared to including it directly in makeChart.
$scope.projZoneChart = AmCharts.makeChart("projZoneChart", chartOps);
$scope.projZoneChart.dataProvider = chartData;
$scope.projZoneChart.validateData(); //required if you're doing it this way but unneccessary overhead compared to simply including it inside of makeChart directly as you're essentially remaking the chart after you create it for the sake of rendering your data.
2) Make sure your *field properties match. startOfWeek isn't mentioned in your chart config at all, even though you're saying it's in your JSON data. Your valueField is set to "value" - you might want to set it to "startOfWeek" instead unless you're modifying your JSON object somewhere else.
3) Displaying charts inside a modal or other dynamic/hidden elements typically require that you call the chart object's invalidateSize method when the modal/tab/etc containing the chart is visible so that it will render correctly. You'll want to check for whatever event md-dialog offers to determine when it is visible before calling $scope.projZoneChart.invalidateSize().

How to set C3JS tooltip content

I have a situation similar to the example shown in http://c3js.org/samples/data_json.html My simple intention is to get the name of the row (i.e. 'www.site1.com') into the tooltip header. My problem: I cannot find it in the d-values.
Can anyone help?
You cannot find it in d-values, because it's not a value actually - it's a category.
Have a look at Category Axis example, maybe it helps you:
var chart = c3.generate({
data: {
columns: [
['data1', 30, 200, 100, 400, 150, 250, 50, 100, 250]
]
},
axis: {
x: {
type: 'category',
categories: ['cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6', 'cat7', 'cat8', 'cat9']
}
}
});
I solved it by using the d-value to address the right category of my JSON, or rather the array in my JSON. (Seemed more complicated since I load the data after I create the chart.)

tablesorter with scroller widget not sorting fixed columns

I'm trying to set up freeze pane (sticky header and fixed columns) using the scroller widget. When I add it to my table the sorting "breaks" in the following way: clicking on one of the fixed columns headers (first or last name in my table), sorts by one of the non fixed columns.
Any ideas?
Thanks
jsfiddle
$('#spreadsheet_table').tablesorter({
widgets: [ 'scroller' ],
widgetOptions : {
scroller_fixedColumns : 2,
scroller_addFixedOverlay : true,
scroller_rowHighlight : 'hover',
scroller_barWidth : null
}
});
$('#spreadsheet_table').tablesorter();
$('#spreadsheet_table').trigger('update');
var sorting = [[1, 0]];
setTimeout(function () {
$('#spreadsheet_table').trigger('sorton', [sorting]);
}, 100);
Use the sortList option as an initialization option instead of trying to trigger a sort after tablesorter has initialized - demo
$('#spreadsheet_table').tablesorter({
theme: 'blue',
sortList: [[1, 0]],
widgets: ['scroller'],
widgetOptions: {
scroller_fixedColumns: 2,
scroller_addFixedOverlay: true,
scroller_rowHighlight: 'hover',
scroller_barWidth: null
}
});

kendo chart display 0 value

I display correctly a funnel dataviz kendo-Ui chart, it works great except when my dataItem is 0
I want it to display in my label so I made a custom label:
seriesDefaults: {
labels: {
visible: true,
background: "transparent",
color:"white",
format: "N0",
template: "#= dataItem.type # - #= dataItem.value#"
},
dynamicSlope: false,
dynamicHeight: false
},
I get my data from a Json call which return 0.
How should I do to make my funnel display the label even if the dataItem.value = 0?
I ended by doing quick and dirty fix, i replaced 0 by 0.000001 and rounding at display it's dirty but it works.

Tablesorter Range Slider Values to Header - Initial Values Not Showing

When I use the filter-formatter uiRange function and set valueToHeader to true, no values show initially. If I move one of the sliders then the range of values show. If I set valueToHeader to false, everything displays fine initially and afterwards.
This issue seems to appear sometime after tablesorter version 2.7.8.
Below is a snippet of how my code looks:
1 : function($cell, indx)
{
return $.tablesorter.filterFormatter.uiRange( $cell, indx, {
values: [0, 300],
min: 0,
max: 300,
delayed: false,
valueToHeader: true,
exactMatch: true
});
},
Thanks for any help.
Regards,
lindon

Resources