Having ColumnSeries and a LineSeries within the same plot (CategoryAxis problem)

Oystein Bjorke 10 років тому оновлено Chris 8 років тому 1
This discussion was imported from CodePlex

mezeizsolt wrote at 2013-03-21 14:17:

Hi!
First of all I want to thank you for this great project, it saved me a lot of work already, its features are great!

I have run into a problem recently and I need some help: I have to show a ColumnSeries and a LineSeries on the same plot. The x-axis has to be time, and with the LineSeries out of the picture it works fine:
public class PlotData
{
        public DateTime TimeOnX { get; set; }
        public double DataOnY { get; set; }
        public string LabelOnX { get { return this.TimeOnX.ToString("yyyy.mm.dd"); } }
}
var plot = new PlotModel();
plot.Axes.Add(new CategoryAxis { ItemsSource = PlotDataItems, LabelField = "LabelOnX" });
plot.Axes.Add(new LinearAxis(AxisPosition.Left) { MinimumPadding = 0, AbsoluteMinimum = 0 });
plot.Series.Add(new ColumnSeries { ItemsSource = PlotDataItems, ValueField = "DataOnY" });
After adding LineSeries (plot.Series.Add(new LineSeries { ItemsSource = PlotDataItems, DataFieldX = "TimeOnX", DataFieldY = "DataOnY" });) I get a perfecly vertical line (every value is assigned to the "category of nothing").
Then I figured out I have to set the ItemsSource of the CategoryAxis a collection of DateTime instead of PlotData:
plot.Axes.Add(new CategoryAxis { ItemsSource = PlotDataItems.Select(x => x.TimeOnX), Labels = PlotDataItems.Select(x => x.LabelOnX).ToList() });
As it turns out, LabelField property cannot be null, however I want to use the DateTime object itself, not its properties. (I am not quite sure how it supposed to work exactly, I could not find any details.)

Sorry, if I have overlooked something important.

Thank You,
Zsolt

Hello Zsolt,

even if you won't read this after 2 years, for other ones with a similar issue...Using the DateTimeAxis as x-axis, you can directly assign a string format to it, so you won't need your property 'LabelOnX'. Then it's just a common scenario.


Even if you have to use both properties, using two x-axis(DateTimeAxis and CategoryAxis) could be a workaround(not very nice I admit). Then one axis has to overlay the other one.