-
Notifications
You must be signed in to change notification settings - Fork 11.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to set different line colors based on marker values and color in chart js line graph #11204
Comments
@ttvettuparambil I think you could the |
@ttvettuparambil another possible solution is to use chartjs-plugin-gradient. |
Hello stockiNail, thank you for the reply. I have managed to use segments to bring colors in different segments but still have not gotten the required result. currently I have modified my code as can be seen in the link below. I am trying to use ctx object to access p0 and p1 points and give a gradient.addColorStop to p0 and p1 points so that theoretically the gradient should appear in each segment depending on the color of the points, but unfortunately let gradient = ctx.createLinearGradient(ctx.p0.x, ctx.p0.y, ctx.p1.x, ctx.p1.y) in test() is having an error in console although I can access p1 and p1 objects just fine. |
const test = (ctx) => { |
1 similar comment
const test = (ctx) => { |
On logging the ctx object in console window for both segment{ borderColor } as well as datasets { borderColor } I have found that addcolorstop property is not available in segment{ borderColor }. So I guess segment method to add gradient for line is out of the question. Can you show me as to how to implement chartjs-plugin-gradient in vanilla js. |
I have found the solution (kind of.) Since gradient is not possible in ctx (context) object in borderColor in segments object, I decided to apply a solid color for each segment in the graph with the color changing based on the ctx.p1.options.backgroundColor value. This is the code that I have used to acheive this. function drawCustomScoreChart(panel) {
} |
@stockiNail @can you give me a working example of this plugin along with a quick summary on the plugin options ? |
@ttvettuparambil apologize for late reply but I was busy on other stuff. To have the canvas context, you should access to the chart instance, as following: const canvasContext = ctx.chart.ctx;
canvasContext.createLineagGradient... |
Yes, I can but let me take time a bit. But having a look to your code, I see (but maybe I'm wrong) that the gradient must be applied to the single segment and based on the values of p0 and p1, therefore the plugin is not a good solution for this use case because the plugin is applied to the whole line based on values of dataset. |
I didn't test it but it should be: const test = (context) => {
const {chart, p0, p1} = context;
const ctx = chart.ctx;
let gradient = ctx.createLinearGradient(p0.x, p0.y, p1.x, p1.y);
gradient.addColorStop(0, p0.options.backgroundColor);
gradient.addColorStop(1, p1.options.backgroundColor);
return gradient;
} |
Hello @stockiNail, Thank you for your response, Seems however the snippet is not working properly. I called the function in segment: { borderColor } and put the snippet but now it is rendering only green color gradient. This is the screenshot of the output now. segment: { |
Ok thank you for clarifying this point for me. |
On logging the const ctx = chart.ctx; I dont see any p0 and p1 objects in the output. So I am not sure how the green gradient color is even rendering. |
yes, because p0 and p1 are int not chart.ctx. Anyway I have created a codepen and I have got the same weird behavior that only the last gradient is applied. |
@stockiNail, so basically the above snippet will not satisfy the requirement. Is there any other method that you could think of that can be used to solve the requirement. |
@ttvettuparambil no at the moment, may be anything will come later to my mind. Anyway there is something weird that I cannot understand. |
ok. Thanks a lot for the clarifications. Hope you have a nice day. Do let me know if this feature is implemented in chart.js in the future because as far as I see, no library has implemented this. |
Nice day to you too. I don't give up! I need to know why it's not working! |
@ttvettuparambil have a look to the codepen: https://codepen.io/stockinail/pen/rNZgxae Chart.js/src/helpers/helpers.segment.js Lines 348 to 350 in 82aca78
The changed style is applied if the JSON stringify value of prev and new values are different. But the stringify of a gradient is always and empty object string representation. As workaround, as you can see in codepen, I set a random value in order it's different for all segments and then the JSON stringify is also different then the gradient is always applied. Play on it if you can and let me know. In the meanwhile I have to find time to fix it. |
@stockiNail you are a legend. Put the function in my code and working like a charm. Can't tell you how much I appreciate this. Do let me know when the code bug is fixed. Was racking my brain for past few days to find a solution for this issue. |
Could we leave this issue open for 1 more day. Just in case I have any doubts. |
Thank you @ttvettuparambil |
@ttvettuparambil remember that when the PR will be released you should remove the random value from gradient instance to be clean |
@stockiNail, ok sure. will look out for the new release. Thank you so much. |
@ttvettuparambil PR submitted: #11217 |
@stockiNail that's great news. Never thought the gradient requirement would be possible. Will recommend chart js to anyone and will be my go to library for the future. One quick query: I should only remove the random gradient value; the rest of the code stays the same right? |
YES! The rest will remain the same. To be more precise (see comment): const test = (context) => {
const {chart, p0, p1} = context;
const ctx = chart.ctx;
const {x: x0} = p0.getProps(['x'], true);
const {x: x1} = p1.getProps(['x'], true);
const gradient = ctx.createLinearGradient(x0, 0, x1, 0);
gradient.addColorStop(0, p0.options.backgroundColor);
gradient.addColorStop(1, p1.options.backgroundColor);
gradient.random = Math.random(); /// <--- This row could be removed after PR will be released
return gradient;
} Is it also clear to you why the code is accessing to the p0/p1 X properties by getProps instead of using directly the properties? |
@stockiNail , I confess I do not know how the above code snippet works. Could you please explain it to me? |
const test = (context) => {
const {chart, p0, p1} = context;
const ctx = chart.ctx;
const {x: x0} = p0.getProps(['x'], true);
const {x: x1} = p1.getProps(['x'], true);
const gradient = ctx.createLinearGradient(x0, 0, x1, 0);
gradient.addColorStop(0, p0.options.backgroundColor);
gradient.addColorStop(1, p1.options.backgroundColor);
return gradient;
} I think the last part doesn't need any comment. Be aware that the code is creating a gradient where the color is ONLY horizontally distributed (see |
@ttvettuparambil just FYI, I used the same code you have as segment callback in the PR as additional test case for Chart.js: |
Expected behavior
The line chart should render with the line color changing based on the marker colors.
This is an example screenshot of what the output should be.
https://pasteboard.co/6PKRCJzsaKa6.png
Currently the code that I have written using chart JS (v 4.2.1) is not rendering in the browser.
I am trying to do this using vanilla javascript. I am welcome to any suggestions. If there is an alternate method, kindly suggest it to me also.
Current behavior
The code is not rendering in the frontend and the there is error in console window. (screenshot attached below.)
https://pasteboard.co/Rma2XuY1jAz7.png
Reproducible sample
https://codepen.io/Thomas-1/pen/oNPVqQp
Optional extra steps/info to reproduce
No response
Possible solution
No response
Context
This is an example screenshot of what I am trying to achieve.
https://pasteboard.co/HgPrWMUziKIS.png
chart.js version
v4.2.1
Browser name and version
Microsoft Edge Version 111.0.1661.44 (Official build) (64-bit)
Link to your project
No response
The text was updated successfully, but these errors were encountered: