Stem Plots

Need help in implementing some specific function to your LightningChart Ultimate powered application? Post a question and get code snippets from other LightningChart Ultimate community members.

Moderator: Queue Moderators

Post Reply
davidvandebunte
Posts: 4
Joined: Thu Apr 10, 2014 4:29 pm

Stem Plots

Post by davidvandebunte » Thu Apr 10, 2014 4:55 pm

What is the recommended way to create a stem plot in LightningChart Ultimate? In particular, the graph that MATLAB would make with the "stem" command?

The values on the x-axis may not have equal spacing, suggesting a PointLineSeries. But is there an option to drop a line from the point to the x-axis? I didn't see one.

A bar chart would be a decent solution, but I don't like suggesting the points have width of any kind. It may also look odd without even spacing on the x-axis.

User avatar
ArctionPasi
Posts: 1367
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland
Contact:

Re: Stem Plots

Post by ArctionPasi » Thu Apr 10, 2014 5:29 pm

Hello,

One way to do it is to use a PointLineSeries for the points and LineCollections for the sticks:

Code: Select all


        void MakeStemPlot()
        {
            m_chart = new LightningChartUltimate();
            m_chart.Parent = this;

            m_chart.Dock = DockStyle.Fill;
            m_chart.BeginUpdate();


            m_chart.ViewXY.XAxes[0].ValueType = AxisValueType.Number;
            m_chart.ViewXY.LegendBox.Visible = false; 

            const int PointCount = 30;
            
            //Add series for points only, not line
            PointLineSeries seriesForPoints = new PointLineSeries(m_chart.ViewXY, m_chart.ViewXY.XAxes[0], m_chart.ViewXY.YAxes[0]); 
            seriesForPoints.PointsVisible = true; 
            seriesForPoints.LineVisible = false; 
            m_chart.ViewXY.PointLineSeries.Add(seriesForPoints); 

            SeriesPoint[] points = new SeriesPoint[PointCount];
            Random rand = new Random(); 

            for (int i = 0; i < PointCount; i++)
            {
                double x = i + rand.NextDouble()*0.6;

                points[i].X = x;
                points[i].Y = 3 + Math.Sin(x/5.0) * 10.0; 
            }
            seriesForPoints.Points = points;

            //Add vertical sticks 
            LineCollection stickCollection = new LineCollection(m_chart.ViewXY, m_chart.ViewXY.XAxes[0], m_chart.ViewXY.YAxes[0]);
            stickCollection.LineStyle.Width = 1; 
            stickCollection.LineStyle.Color = Color.White; 

            SegmentLine[] lines = new SegmentLine[PointCount];
            
            for (int i = 0; i < PointCount; i++)
            {
                lines[i].AX = points[i].X;
                lines[i].AY = points[i].Y;

                lines[i].BX = points[i].X;
                lines[i].BY = 0;
            }

            stickCollection.Lines = lines;
            m_chart.ViewXY.LineCollections.Add(stickCollection);

            m_chart.ViewXY.FitView();
            m_chart.EndUpdate();
        }
Stem plot with LightningChart
Stem plot with LightningChart
stem_plot_chart.jpg (111.15 KiB) Viewed 9708 times
I've used irregular spacing by random value.

Like this?
LightningChart Support Team, PT

davidvandebunte
Posts: 4
Joined: Thu Apr 10, 2014 4:29 pm

Re: Stem Plots

Post by davidvandebunte » Thu Apr 10, 2014 7:05 pm

Yes, that should do it!

Thanks.

Post Reply