Comments (10)
Thank you.
Yes, that is possible. How the ColorTemplate
works is described in the ReadMe.md file.
For example, if you have a ChartData
object with 2 DataSets
, and each DataSet should have 2 different colors, you need to do the following:
ColorTemplate ct = new ColorTemplate();
ct.addDataSetColors(new String[] { R.color.red, R.color.blue }, Context);
ct.addDataSetColors(new String[] { R.color.green, R.color.yellow }, Context);
mChart.setColorTemplate(ct);
After that, the first DataSet
will have the colors "red" and "blue", and use them alternately. The second DataSet
will have the colors "green" and "yellow" and also use them alternately.
Changing the background color below the lines is currently not supported.
Be aware that the ColorTemplate
will be deprecated in the next update and colors can then be set via DataSet
directly. This implementation should then be much clearer and easier to use.
Here is the full explanation:
Setting colors:
Setting colors can be done via the ColorTemplate
class that already comes with some predefined colors (constants of the template e.g. ColorTemplate.LIBERTY_COLORS
).
Explaination: The ColorTemplate
basically has two methods for setting colors:
addDataSetColors(int[] colors, Context c)
: This method will add a new array of colors for theDataSet
at the current index. The current index starts at 0 and counts up per call of this method. If no calls of this method have been done before, the colors set in this call will be used for theDataSet
at index 0 in theChartData
object. Upon calling this method again on the sameColorTemplate
object, the provided color values will be used for theDataSet
at index 1.addColorsForDataSets(int[] colors, Context c)
: This method will spread the provided color values over an equal amount ofDataSet
objects, using only one color perDataSet
.
In our example case, we want one color for each DataSet
(red and green), which will mean, that all entries belonging to the same DataSet
will have the same color:
ColorTemplate ct = new ColorTemplate();
ct.addColorsForDataSets(new int[] { R.color.red, R.color.green }, this);
chart.setColorTemplate(ct);
It would also be possible to let each DataSet
have variations of a specific color. For example company 1 should have 4 colors from light to dark red, and company 2 should have 4 colors from light to dark green. In that case, we specify a color array for each DataSet
:
ColorTemplate ct = new ColorTemplate();
ct.addDataSetColors(redColors, this); // redColors is an array containing 4 colors
ct.addDataSetColors(greenColors, this);
chart.setColorTemplate(ct);
from mpandroidchart.
Hi, thanks for your explanations :)
The only problem is that : okay you can alternate different colors for the same line, but it renders the same color for the circle and the line that follows it. The actual pattern of the color alternation is [circle+line][cirlce+line] etc but what I want is [cirlce][line][cirlce][line].
from mpandroidchart.
In other words, I would like this type of render, but I had to modify the LineChart sourcecode to obtain it (the drawData() method especially).
from mpandroidchart.
Allright now I understand.
I am currently working on a new model that will allow you to set one (or more) colors for a line, and one (or more) colors for the circles, completely independant of each other.
You can already see this on the latest commits of the experimental branch.
Then your requirements should be fulfilled.
from mpandroidchart.
Nice :) thanks a lot for your answers. For the moment I will keep modifications I made and I will adopt your new model when it will be available. Sorry if I wasn't clear :)
Continue the great work !
from mpandroidchart.
Hi,
Thanks for the great library!
Is it already possible in the release branch to choose different colours for 1 line?
Thanks
Wouter
from mpandroidchart.
same question as @woutergoossens.
from mpandroidchart.
I have one doubt. I want two different colours when My data increase and decrease. Suppose when my line chart is moving upward, I want green colour of that line . And when it is moving downward, I want red colour. Any help please
from mpandroidchart.
@avlemos , I have same question , Did you find any solution ? I want to have two colors for one dataset based on Y - axis values, but dataset.setColors method not working.
from mpandroidchart.
@PhilJay How can I highlight the alternative lines...? For example ,In X axis ,0 should be darken and 1 should be lighten..and again 2 should be darken...How to attain this ??
from mpandroidchart.
Related Issues (20)
- combined chart issue
- Update repository
- Animate only selected bars in BarChartSelectable
- Android Studio: disabled warning when using SDK features unavailable at SDK 21 (e.g. @FunctionalInterface or streaming)? HOT 1
- Barchart is not displaying properly for 15 days. HOT 2
- Granularity is not working when set to 6f.
- Highlight issues after i applied RoundedSlicesPieChartRenderer in piechart
- 可以设置初始化Y轴数据的数量 之后一个一个的动态加载吗?
- Help: Dotted Background
- 使用implementation ("com.github.PhilJay:MPAndroidChart:v3.0.2")报错
- Values above the Horizontal bars Disappears when I zoomed in
- dataSet.gradientColors = gradientColors not working
- Fatal Exception: java.lang.NegativeArraySizeException HOT 4
- LineDataSet: shader with multiple color
- Getting crash while selection camera cropping only in Android 10
- How to rotate the Tooltip arrow Based on the touch points
- Documentation Link Failed! HOT 4
- When drawing Bezier curves vertically in LineChart, the curve flickers.
- Fatal Exception: java.lang.NullPointerException Attempt to read from null array
- Fatal Exception: java.lang.ArrayIndexOutOfBoundsException
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mpandroidchart.