Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I overlay plots so that I can see what is behind each plot?

I have created a HistogramPlot by inheriting from WaveformPlot. In my plot class I call FillRectangle(...) to make the bars.

Now I need to put more than one histogram on the same graph, and I need to see bars that are behind other bars. So to do this I need to draw an outline of the bars in the color of the bar over the top of another bar when the smaller bar is shorter.

This means I need some kind of AND/OR function to combine colors. Because there is one WaveformPlot per plot, the last plot to draw covers the previous plots and there is no way to make the bars from the other plots bleed through.

Does anyone have any idea how to deal with this? When plotting normal waveforms (lines), this does not matter because the lines only cross at a few pixel points and nothing is hidden. But with bars, it is a serious problem.
0 Kudos
Message 1 of 3
(3,115 Views)
The Color type in the .NET Framework allows you to specify the Alpha component of the color. The alpha component of the color determines its transparency. For example, you can create a semi-transparent red by calling:

Color.FromARGB(128, Color.Red)
Abhishek Ghuwalewala | Measurement Studio | National Instruments
0 Kudos
Message 2 of 3
(3,109 Views)
Thanks. Works like a charm. I have pasted my code for anyone that wants it:

using System;
using System.Drawing;
using System.Collections.Generic;
using NationalInstruments.UI;
using NationalInstruments.Analysis.Math;

namespace com.Linear.ATE.Data
{
    public class BarPlot : WaveformPlot
    {
        private Color color;
        private int transparency = 50;
        private List<RectangleF> bars = new List<RectangleF>();
        private bool spline = true;

        public BarPlot(XAxis xAxis, YAxis yAxis, Color color, int transparency) : base(xAxis, yAxis)
        {
            this.color = color;
            this.transparency = transparency;
        }

        public bool UseSpline
        {
            get { return spline; }
            set { spline = value; }
        }

        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

        public int Transparency
        {
            get { return transparency; }
            set { transparency = value; }
        }

        public List<RectangleF> Bars
        {
            get { return bars; }
        }

        protected override void OnAfterDraw(AfterDrawXYPlotEventArgs e)
        {
            base.OnAfterDraw(e);

            Color c = Color.FromArgb(255*transparency/100, color);
            Brush barBrush = new SolidBrush(c);

            Graphics g = e.Graphics;

            // Clip the data buffer to the points that are in the current range.
            double[] xData = null, yData = null;
            ClipDataPoints(out xData, out yData);

            List<double> x = new List<double>();
            List<double> y = new List<double>();
            bars.Clear();
            for (int i = 0; i < xData.Length; ++i)
            {
                // Map the current data point to the corresponding screen coordinate.
                PointF dataPoint = MapDataPoint(e.Bounds, xData[i], yData[i]);

                PointF top = this.MapDataPoint(e.Bounds, xData[i], yData[i]);
                PointF bottom = this.MapDataPoint(e.Bounds, xData[i], 0.0);

                RectangleF r = new RectangleF(top.X + 1,
                    top.Y,
                    (float)(e.Bounds.Width / xData.Length) - 1,
                    bottom.Y);
                x.Add(r.X + r.Width/2);
                y.Add(r.Y);
                   g.FillRectangle(barBrush, r);
                bars.Add(r);
            }
            if (spline)
            {
                double[] xVals = x.ToArray();
                double[] yVals = y.ToArray();
                CurveFit.CubicSplineFitInPlace(xVals, yVals, 2.0);
                PointF[] points = new PointF[xVals.Length];
                for (int index = 0; index < xVals.Length; index++)
                    points[index] = new PointF((float)xVals[index], (float)yVals[index]);
                g.DrawLines(new Pen(color, 2), points);
            }
        }

    }
}

0 Kudos
Message 3 of 3
(3,091 Views)