简体   繁体   中英

Different colors for each value in a ColumnSeries

I am using LiveCharts in WPF to create a Cartesian Chart with columns. The values come from a list which in turns is populated with values from a database.

Everything works fine except I would like each column to have a different fill. It does not matter what color, just a random color for each seperate column. Also, I do not want to create a new series for each value because there are too many values.

The Mappers method from the similar question does not work for me since the example changes color according to how high the value is. I want different colors for each different axis label.

This is the part of my code where I create the series:

cartesianChart.Series.Add(new ColumnSeries
{
    Title = "Column Chart: ",
    Fill = Brushes.CadetBlue,
    Values = valuesList.AsChartValues(),
    DataLabels = true,
    LabelPoint = point => (point.Y).ToString(),
});

This is what the chart looks like:

图表

Any ideas would be appreciated.

try DataPoint class:

DataPoint p=new DataPoint(chart1.Series["Celler"]);
p.SetValueY(cellVoltage[cell]);
p.Label=cellname[cell];//string
p.Color=Color.FromArgb(255 - cell * 20, 255, 80cell*20);
chart1.Series["Celler"].Points.Add(p);

If you use a SeriesCollection in LiveCharts it will automatically assign a different color to every individual series in the collection.

For this example I have taken a snippet from one of my projects which adds multiple series and has a separate List which contains all the series names. This will loop and add each series in to the collection and add the series data and series name from the list.

Live Charts Geared Example:

SeriesCollection = new LiveCharts.SeriesCollection();

List<GearedValues<long>> seriesList = new List<GearedValues<long>>();

seriesList.Add(Data1.AsGearedValues().WithQuality(Quality.Low));
seriesList.Add(Data2.AsGearedValues().WithQuality(Quality.Low));

for(int i = 0; i < seriesName.Count(); ++i)
{
    SeriesCollection.Add(new GColumnSeries
    {
        Values = seriesList[i],
        Name = seriesName[i],
        Fill = System.Windows.Media.Brushes.Transparent,
        StrokeThickness = 1,
        PointGeometry = null,
        LineSmoothness = 0,
    });
}

Standard Live Charts Example

SeriesCollection = new LiveCharts.SeriesCollection();

List<ChartValues<long>> seriesList = new List<ChartValues<long>>();

seriesList.Add(Data1);
seriesList.Add(Data2);

for(int i = 0; i < seriesName.Count(); ++i)
{
    SeriesCollection.Add(new ColumnSeries
    {
        Values = seriesList[i],
        Name = seriesName[i],
        Fill = System.Windows.Media.Brushes.Transparent,
        StrokeThickness = 1,
        PointGeometry = null,
        LineSmoothness = 0,
    });
}

The SeriesCollection colors are preset for each series in the collection, however these default colors can be modified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM