score:27
To use your own colours you will have to override the existing colours, I prefer not to tinker around with the original code.
So this is what I did.
var myColors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"];
d3.scale.myColors = function() {
return d3.scale.ordinal().range(myColors);
};
nv.addGraph(function() {
var chart = nv.models.pieChart()
.x(function(d) { return d.label })
.y(function(d) { return d.value })
.showLabels(true).color(d3.scale.myColors().range());
d3.select("#chart svg")
.datum(data)
.transition().duration(1200)
.call(chart);
return chart;
});
All I did was add .color(d3.scale.myColors().range())
UPDATE :
Check answer by Christopher Chiche, for the perfect solution.
.color(['blue', 'green', 'yellow'])
Hope this helps.
score:0
Here is a note that got me, I was setting chart.color inside of nv.addGraph, this was not working correctly and would instead use the defaults. I then set it outside of this and it worked fine.
score:0
I use .color(function(d) {return [d.value > 0 ? 'blue' : 'red']});
to make it change color depending on data value.
Hope this help someone.
score:0
Here is the Typescript solution which is similar to the JS version. Let's say you want a Top 10 of your results with 10 different colors :
Here i'll give you the example that work for my PieChart or MultiBarChart
- Create a global array of color like colors = ["#D9D9D9", "#4B11A6", ....];
- The data has to be formatted in an Object : myGraph { key: "Title", values : { streams to push }}
- Then when creating your data table just push the color for each stream :
let stream = {}; let data = []; let i=0;
dataTable.forEach((period) => {
stream = { 'label': period.key, 'value': period.value, 'color': this.colors[i] };
if(period.value !== 0) {
data.push(stream);}
i++;
});
this.myGraph = data;
The if condition is just there to prevent empty piece in the graph
score:0
I have used below concept to add custom color:
// for define variable of custom colors
var colors = ["red", "green", "blue"];
...
chart {
color: function(d,i){
return (d.data && d.data.color) || colors[i % colors.length]
}
}
For dynamic color based on JSON Data just add New json obj color
such as below concept..
// or set custom colors directly in the data
data = [{
key: "One",
y: 5,
color: "yellow"
},{
key: "Two",
y: 2,
color: "gray"
},{
key: "Three",
y: 9
},
...
];
score:44
You can add colors by passing an array to the 'color()' option. So just add:
.color(['blue', 'green', 'yellow'])
If you want to use these colors for 3 elements.
Note: if you have more than 3 elements, then some colors will be used multiple times.
score:50
If you want to use specific color for pie
chart.color(function(d){
return d.data.color
});
Then, organize your data as:
[
{
key: "Cumulative Return",
values: [
{
"label": "One",
"value" : 29.765957771107,
"color" : "#8c564b"
} ,
{
"label": "Three",
"value" : 32.807804682612,
"color" : "#e377c2"
}
]
}
]
Source: stackoverflow.com
Related Query
- How to customize color in pie chart of NVD3
- how can set customize color to pie chart using D3.js?
- How do I color labels for my pie chart in D3?
- How to customize legend text in pie chart for angular-nvd3?
- NVD3 Cumulative Line Chart : How to set different back ground color for specific X axis
- How can I draw pie chart with custom color in d3.js
- How can I change the colors of the NVD3 Pie Chart
- How to update percent values of custom tooltip in angular nvd3 pie chart
- how to update nvd3 pie chart and resize it
- How to set text color for my d3 chart title?
- How to customize the color scale in a D3 line chart?
- 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?
- How to add drop shadow to d3.js pie or donut chart
- How can I keep tick marks from repeating when I have a small number of dates on an nvd3 chart
- How to add a nice legend to a d3 pie chart
- How to obtain percentage in tooltips of an nvd3 pie chart?
- how to add legend to a pie chart using D3js? And how to centralise the pie chart?
- How to display values in Stacked Multi-bar chart - nvd3 Graphs
- Same color is showing in different arc of d3 pie chart
- Responsive pie chart using NVD3
- How can I add a click event to show a tooltip in an NVD3 Pie Chart?
- How to add space between bars in a grouped bar chart in a nvd3 grouped multibar chart?
- How can I rotate d3 chart and how can I start pie chart from angle -90?
- How to change the viewfinder (focus chart) of a NVD3 line chart programmatically?
- How to move labels to outside pie chart in D3
- How to display the percentage % on a NVD3 Pie Chart?
- nvd3 stacked area chart looks glitchy how to fix?
- nvd3 pie chart says Uncaught TypeError: Cannot call method 'map' of undefined
- How to correctly centre text element on all bars of a nvd3 bar chart
- How to add events to the individual sections(arcs) of a pie chart created out of d3.js?
More Query from same tag
- d3.js link seperation in tree layout
- reading in CSV with D3 js
- Facebook web data API and D3.JS
- Why is D3 Zoom function not working in Safari?
- Transitioning the clipPath when the data is updated
- d3.js - Change link colour to child node depending on child d.data.type
- d3js how to select the element i'm currently working on
- Hiding one line on a D3 plot
- dc.js row chart with negative values doesn't show full rows
- D3.js: Elements don't have any width
- Grouping Circles and Path as a node in D3 Force Layout?
- Introduce gap in path in d3
- maps and routes in D3
- D3 Bubble Example: What does bubble.nodes() do?
- D3 map displaying country name
- d3 tree problems updating last node
- Why does my JSON update for one API but not the other?
- How do I create a minimap of an SVG using D3 v5 and show the current viewport dimensions?
- D3 js tooltip violin plot
- d3 text perpendicular to inner radious of an arc
- d3 drag path does not work
- creating word cloud with d3.js
- D3 line acting as a closed path
- Show all dates between starting date and ending date on the x-axis
- Reading csv data file with c3.js
- D3.js ~ placing rectangles with for loop and coloring them
- Use months instead of year in D3plus.js .time()
- Recentering D3 force layout diagram on node click
- D3.JS add zoom and listitems to rect
- What is the correct way to draw lines direction by conditional