-1

Is it possible to display a LineSeries X/Y graph that contains only one point? For two or more points, it works great, but one point is not shown even PointGeometry is set to some shape. At least in this variant, I would expect to see a single point for one-point series. Is there any way to enable it?

EDIT: I'm really sorry for the slow response, I wasn't at the computer. Sach's answer moved me, but I still have no solution. Look at my code - modification of Sach's sample. If I use my own data type and formatter for the axis, it only works with one point if it is specified in the constructor. If I create an empty serie, will the graph show up when I add at least two points? (see handler mouse down)

public partial class MainWindow : Window
    {
        LineSeries lineSeries;
        public SeriesCollection SeriesCollection { get; set; }
        public Func<double, string> XFormatter { get; set; }
        public Func<double, string> YFormatter { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            var dayConfig = Mappers.Xy<DateModel>()
                .X(dayModel => (double)dayModel.DateTime.Ticks / TimeSpan.FromHours(1).Ticks)
                .Y(dayModel => dayModel.Value);

            lineSeries = new LineSeries()
            {
                //Values = new ChartValues<DateModel>{ new DateModel(DateTime.Now, 3) }, //this works
                Values = new ChartValues<DateModel>(),
                PointGeometry = DefaultGeometries.Circle
            };

            SeriesCollection = new SeriesCollection(dayConfig);
            SeriesCollection.Add(lineSeries);

            XFormatter = value => new System.DateTime((long)(value * TimeSpan.FromHours(1).Ticks)).ToString("HH:mm");
            YFormatter = value => value.ToString("0.0000");

            DataContext = this;
        }


        public class DateModel
        {
            public DateModel(DateTime timestamp, double value)
            {
                DateTime = timestamp;
                Value = value;
            }

            public System.DateTime DateTime { get; set; }
            public double Value { get; set; }
        }

        private void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            lineSeries.Values.Add(new DateModel(DateTime.Now, 3));
        }
    }
Viliam
  • 636
  • 9
  • 17
  • Please edit the question and post your code. – Sach Jun 18 '19 at 20:48
  • As provided by Sach, it is working, if you using a point you can use scatter series instead of a line. Check also if you specify the point shape, maybe you null it. – Kaspar Jun 19 '19 at 09:02
  • Please check my comment, without the formatter and own data model it works statically and dynamically, but with them, for one point only statically – Viliam Jun 19 '19 at 11:40

1 Answers1

1

It works by default. This is a simple LineSeries with only one point:

public partial class MainWindow : Window
{
    public SeriesCollection SeriesCollection { get; set; }
    public MainWindow()
    {
        InitializeComponent();

        SeriesCollection = new SeriesCollection
        {
            new LineSeries
            {
                Values = new ChartValues<double> { 3 }
            }
        };

        DataContext = this;
    }
}

And XAML:

<Window x:Class="SOLineCharts.MainWindow"
        ....
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <lvc:CartesianChart Series="{Binding SeriesCollection}" />
    </Grid>
</Window>

Result:

enter image description here

Sach
  • 10,091
  • 8
  • 47
  • 84