Jump to content

Help with Javascript and CSS graph?

Go to solution Solved by Mr_KoKa,

No, fillColor is used originally to create new bar, but we modified so it's uses values from data so your code that initializes your chard should look like this:

var barChartData = {		labels : ["Integritas","Optimus","Spirare","Virtus"],		datasets : [			{				fillColor : "rgba(220,0,0,1)",				strokeColor : "rgba(0,0,0,0.8)",				highlightFill: "rgba(200,200,0,0.75)",				highlightStroke: "rgba(0,0,0,1)",				data : [{value: 1, color: '#999'}, {value: 2, color: '#444'}]			}		]	}

I just noticed I ate one '{' and one apostrophe. sorry, it might be the problem.

 

You may want to fiddle with this lines in the second part:

		highlightFill : dataset.highlightFill || dataset.fillColor,		highlightStroke : dataset.highlightStroke || dataset.strokeColor

so you can control color of the bar after it's beeing highlighted, I'm not sure when it happens, but probably on mouse hover or click.

I am using Chart.js's bar graph here. The code that enters the data and sets the color of the bars is this

var barChartData = {		labels : ["Integritas","Optimus","Spirare","Virtus"],		datasets : [			{				fillColor : "rgba(220,0,0,1)",				strokeColor : "rgba(0,0,0,0.8)",				highlightFill: "rgba(200,200,0,0.75)",				highlightStroke: "rgba(0,0,0,1)",				data : [100,300,400,500]			}		]	}

However, the problem is it sets ALL the bars to the same color. How can I change this to set EACH label to a different color? I tried copying this and setting only one label per dataset but the chart fails to load if I try that.

- i7-2600k @ 4.7GHz - MSI 1070 8GB Gaming X - ASUS Maximus V Formula AC3 Edition - 16GB G.SKILL Ripjaws @ 1600Mhz - Corsair RM1000 - 1TB 7200RPM Seagate HDD + 2TB 7200 HDD + 2x240GB M500 RAID 0 - Corsair 750D - Samsung PX2370 & ASUS ROG SWIFT -

Link to comment
https://linustechtips.com/topic/479584-help-with-javascript-and-css-graph/
Share on other sites

Link to post
Share on other sites

You can modify your Chart.Bar.js file so it will handle data as array of objects where eg. value is value and color is color your data would look like 

[{value: 1, color: '#999'}, value: 2, color: '#444}]

You may want to use rgba notation.

 

You would need to find this in your Char.Bar.js file and modify it:

helpers.each(dataset.data,function(dataPoint,index){	//Add a new point for each piece of data, passing any required data to draw.	datasetObject.bars.push(new this.BarClass({		value : dataPoint,		label : data.labels[index],		datasetLabel: dataset.label,		strokeColor : dataset.strokeColor,		fillColor : dataset.fillColor,		highlightFill : dataset.highlightFill || dataset.fillColor,		highlightStroke : dataset.highlightStroke || dataset.strokeColor	}));},this);

to something like this: (I have not tested it but should work)

helpers.each(dataset.data,function(dataPoint,index){	//Add a new point for each piece of data, passing any required data to draw.	datasetObject.bars.push(new this.BarClass({		value : dataPoint.value,		label : data.labels[index],		datasetLabel: dataset.label,		strokeColor : dataset.strokeColor,		fillColor : dataPoint.color,		highlightFill : dataset.highlightFill || dataset.fillColor,		highlightStroke : dataset.highlightStroke || dataset.strokeColor	}));},this);
Link to post
Share on other sites

I edited it to what you posted but the bar graphs don't show up at all, is there anything else that needs changed as well?

- i7-2600k @ 4.7GHz - MSI 1070 8GB Gaming X - ASUS Maximus V Formula AC3 Edition - 16GB G.SKILL Ripjaws @ 1600Mhz - Corsair RM1000 - 1TB 7200RPM Seagate HDD + 2TB 7200 HDD + 2x240GB M500 RAID 0 - Corsair 750D - Samsung PX2370 & ASUS ROG SWIFT -

Link to post
Share on other sites

You may want to check dev console in your browser to check up if there any arrors show up. If we (you :P) won't figure it out I will download the lib and fiddle with it.

 

You did change data and the second part of code too? The part where we use dataPoint.value and dataPoint.color.

Link to post
Share on other sites

You may want to check dev console in your browser to check up if there any arrors show up. If we (you :P) won't figure it out I will download the lib and fiddle with it.

 

You did change data and the second part of code too? The part where we use dataPoint.value and dataPoint.color.

I changed the second part but I think the problem is I am not using the first part correctly, do I replace the fillcolor with the first part?

- i7-2600k @ 4.7GHz - MSI 1070 8GB Gaming X - ASUS Maximus V Formula AC3 Edition - 16GB G.SKILL Ripjaws @ 1600Mhz - Corsair RM1000 - 1TB 7200RPM Seagate HDD + 2TB 7200 HDD + 2x240GB M500 RAID 0 - Corsair 750D - Samsung PX2370 & ASUS ROG SWIFT -

Link to post
Share on other sites

No, fillColor is used originally to create new bar, but we modified so it's uses values from data so your code that initializes your chard should look like this:

var barChartData = {		labels : ["Integritas","Optimus","Spirare","Virtus"],		datasets : [			{				fillColor : "rgba(220,0,0,1)",				strokeColor : "rgba(0,0,0,0.8)",				highlightFill: "rgba(200,200,0,0.75)",				highlightStroke: "rgba(0,0,0,1)",				data : [{value: 1, color: '#999'}, {value: 2, color: '#444'}]			}		]	}

I just noticed I ate one '{' and one apostrophe. sorry, it might be the problem.

 

You may want to fiddle with this lines in the second part:

		highlightFill : dataset.highlightFill || dataset.fillColor,		highlightStroke : dataset.highlightStroke || dataset.strokeColor

so you can control color of the bar after it's beeing highlighted, I'm not sure when it happens, but probably on mouse hover or click.

Link to post
Share on other sites

No, fillColor is used originally to create new bar, but we modified so it's uses values from data so your code that initializes your chard should look like this:

Ahh that seemed to work! Thank you!

- i7-2600k @ 4.7GHz - MSI 1070 8GB Gaming X - ASUS Maximus V Formula AC3 Edition - 16GB G.SKILL Ripjaws @ 1600Mhz - Corsair RM1000 - 1TB 7200RPM Seagate HDD + 2TB 7200 HDD + 2x240GB M500 RAID 0 - Corsair 750D - Samsung PX2370 & ASUS ROG SWIFT -

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×