Giter Site home page Giter Site logo

Comments (10)

PhilJay avatar PhilJay commented on May 5, 2024

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 the DataSet 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 the DataSet at index 0 in the ChartData object. Upon calling this method again on the same ColorTemplate object, the provided color values will be used for the DataSet at index 1.
  • addColorsForDataSets(int[] colors, Context c): This method will spread the provided color values over an equal amount of DataSet objects, using only one color per DataSet.

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.

HappyDr0id avatar HappyDr0id commented on May 5, 2024

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.

HappyDr0id avatar HappyDr0id commented on May 5, 2024

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).

screenshot_2014-08-13-10-14-36

from mpandroidchart.

PhilJay avatar PhilJay commented on May 5, 2024

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.

HappyDr0id avatar HappyDr0id commented on May 5, 2024

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.

woutergoossens avatar woutergoossens commented on May 5, 2024

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.

avlemos avatar avlemos commented on May 5, 2024

same question as @woutergoossens.

from mpandroidchart.

afreakyelf avatar afreakyelf commented on May 5, 2024

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.

Zephyer7673 avatar Zephyer7673 commented on May 5, 2024

@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.

Govi02 avatar Govi02 commented on May 5, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.