score:0
Accepted answer
After finding code in this answer and the code in responses from this github issue, it was found that d3.selectAll() can be utilized in the callback option for the chart.
$scope.options = {
chart: {
type: 'multiBarChart',
//...,
callback: function(){
d3.selectAll('rect.nv-bar')
.style('fill', function(d, i) {
if (d.y > 75) {
return 'red';
}
if (d.y > 50) {
return 'yellow';
}
return 'green';
});
}
}
}
}
See a demonstration of the changes applied by running the code in the snippet below.
var app = angular.module('plunker', ['nvd3']);
app.controller('MainCtrl', function($scope) {
$scope.options = { /* JSON data */
chart: {
type: 'multiBarChart',
height: 250,
showControls: false,
margin: {
top: 20,
right: 20,
bottom: 45,
left: 45
},
clipEdge: true,
duration: 500,
stacked: true,
xAxis: {
axisLabel: 'Time (ms)',
showMaxMin: false,
tickFormat: function(d) {
return d3.format(',f')(d);
}
},
yAxis: {
axisLabel: 'Y Axis',
axisLabelDistance: -20,
tickFormat: function(d) {
return d3.format(',.1f')(d);
}
},
callback: function() {
//console.log('in callback');
d3.selectAll('rect.nv-bar')
.style('fill', function(data, index) {
//console.log('data.y: ',data.y);
if (data.y > 75) {
return 'red';
}
if (data.y > 50) {
return 'yellow';
}
return 'green';
});
}
},
// title options
title: {
enable: true,
text: 'Title for Line Chart'
},
// subtitle options
subtitle: {
enable: true,
text: 'Subtitle for simple line chart. Lorem ipsum dolor sit amet...',
css: {
'text-align': 'center',
'margin': '10px 13px 0px 7px'
}
},
// caption options
caption: {
enable: true,
html: 'Figure 1. Lorem ipsum dolor sit amet...',
css: {
'text-align': 'justify',
'margin': '10px 13px 0px 7px'
}
}
};
/* Chart data */
$scope.data2 = [{
"key": "Thermal",
"values": [{
"x": 0,
"y": 44
}, {
"x": 1,
"y": 24
}, {
"x": 2,
"y": 66
}, {
"x": 3,
"y": 10
}, {
"x": 4,
"y": 33
}]
}];
});
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.css" />
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-nvd3/1.0.5/angular-nvd3.min.js"></script>
<div ng-app="plunker" ng-controller="MainCtrl">
<nvd3 options="options" data="data2"></nvd3>
</div>
score:0
I used following code to change bar colour... i am putting this code because callback didn't work for me ...u need to change the logic to select bar according to your need
dispatch: {
renderEnd: function (e) {
d3.selectAll("rect.nv-bar").attr('rx', 4).attr('ry', 4).attr('width', 15).style("fill", function (d, i){
if (i%4==0)
{
return "#0064A5"
}
else if (i%3==0)
{
return "#2C76B8"
}
else if (i%2==0)
{
return "#004C76"
}
else{
return "#004180"
}
});
}
}
Source: stackoverflow.com
Related Query
- nvd3.js - display specific color for each bar in multibar vertical chart depending on range of value
- NVD3 Cumulative Line Chart : How to set different back ground color for specific X axis
- How to create multi color vertical bar chart in d3.js using specific json data?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- NVD3 force specific width of bar in bar chart
- How to add space between bars in a grouped bar chart in a nvd3 grouped multibar chart?
- Preview d3 bar chart such that it sorted by values for each x value
- Get unique max value for each y-axis in a bar chart
- d3.js bar chart with pos & neg bars (win/loss) for each record
- Creating a radial chart with a different color for each axis label
- Maintain color mapping for Stacked/Grouped Multi-Bar Chart using NVD3 when the series count fluxuates
- Vertical align long text in yAxis for horizontal bar chart
- draw horizontal lines for bars in nvd3 multi bar chart
- Changing the colors of each of the Stacked Bar chart with different Color in D3
- Is it possible to give specific color for stacks on Bar graph in dimple.js?
- Changing color for each stacked bar in D3.js
- How to assign different color to each bar in the bar chart in d3.js
- Invert xAxis for NVD3 MultiBar Chart
- Display Bar Chart for Last week, Last Month, and Last Year Data from JSON using D3.js
- How to set text color for my d3 chart title?
- How do I display dates on the x-axis for nvd3 / d3.js?
- How to customize color in pie chart of NVD3
- Using an ordinal scale ('d3.scale.ordinal') for the x-axis in a bar chart
- D3js - change Vertical bar chart to Horizontal bar chart
- Maximum width for column in bar chart
- NVD3 line chart with vertical line
- How to display values in Stacked Multi-bar chart - nvd3 Graphs
- Text On each bar of a stacked bar chart d3.js
- D3.js v4 - set padding for bar chart
- d3js Create legend for bar chart
More Query from same tag
- Passing data from Django to D3
- How much secure the data is while using D3.js
- Color overlapped portions of multi line chart
- Select all instance of a word in a rendered webpage
- D3 conditional styling of edges
- Updating text elements in d3.js
- best options for flowing dotted lines (svg path animation) in ie?
- D3 svg chart can't display in IE
- Nested JSON object data with dimple.js chart
- Functions not recognised when using R2D3
- Using .filter() on a horizontal brush
- How to keep the circle, stroke and font size when zooming in d3.js V4 layout?
- Beautify scaleBand
- JS / d3js, calling a CSV; some numbers give NaN error, some don't ... A fix?
- Getting two different output from same json
- D3 sort() results in weird unexpected behaviour
- D3 composite key function
- D3.js version 5 chart to PDF
- Error with D3 data.map function
- Gaps between rectangles around an SVG Circle with D3.js is not universal
- populating the JSON data from spring mvc to d3 chart using request body
- EventDrops for D3.js (from marmelab) - how to tweak example code?
- Change the collision behavior of many nodes stored in an array
- how to change domain and range of d3 graph dynamically?
- D3.js Decision Tree - text wraping, bounding box, # of load circles
- D3 - Can't create line chart with time data
- D3 enter() and draw different component each loop
- dc.js - Tooltips adding another item?
- Add a circle to a d3.js map
- Tooltip Stays Hidden In D3 Treemap