Different colors for each value in a ColumnSeries

点点圈 提交于 2021-01-27 14:41:43

问题


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:

Chart

Any ideas would be appreciated.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/54195346/different-colors-for-each-value-in-a-columnseries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!