ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Barchart in Scattergraph

Solved!
Go to solution

Hello, i have on my User Interface a scattergraph where i plot some points of some colorspaces.

I need in only one configuration an barchart like the following one to draw the raw data of my sensor.

Is it possible to draw some bars with an scattergrap?

barchart.gif

0 Kudos
Message 1 of 7
(5,414 Views)

Hello there,

It is possible to display data as BarChart in ScatterGraph. This is how you can do it.

  1. Add say 3 plots to the ScatterGraph.
  2. Set the LineWidth property of each plot to about 50.
  3. On the XAxis.MajorDivisions, set TickVisible and LabelVisible properties to false.
  4. Set the XAxis.Mode to fixed.
  5. Add 3 custom divisions on XAxis. Set their Text and Value properties. Say for first custom division set Text to 'A1' and Value to '2', similarly set others to (A2, 5) and (A3, 8).
  6. For each ScatterPlot plot with two data points. For example, for ScatterGraph.Plots[0] plot xData = {2, 2} and yData = {0, 8}.Here the xData specifies the data that matches the custom label value. The yData[0] is zero that specifies the base for the each bar in the graph. yData[1] is your data from the color space.

I have attached a sample application to demonstrating the same.

 

Vijet Patankar

National Instruments
Message Edited by vcp on 03-03-2010 04:43 AM
0 Kudos
Message 2 of 7
(5,391 Views)

Hi, big thank you for your answer!

 

I have converted your app to VS.2005 and it works fine for me.

 

Theres only one little Problem, i got the Message

 

NationalInstruments.UI.WindowsForms.ScatterGraph-Typ has no ... with the name UseColorGenerator.   

 

So i removed the following line, but I dont know for what this attribute is required.

this.scatterGraph1.UseColorGenerator = true;

 

 

It works for me, i have added the convertet Project for other users with Visual Studio 2005.

 

 

0 Kudos
Message 3 of 7
(5,354 Views)
Hi there,

I'm glad to know that it is working for you.
The UseColorGenerator is a new property added in Measurement Studio 2009. I think you are using an older version. The property UseColorGenerator assigns new colors to the plots automatically.

Since I used the 2009 version, that line got added. If you're using an earlier version, you can go ahead and remove that line of code and the solution should still work the same.

Vijet Patankar
National Instrument
Message Edited by vcp on 03-07-2010 09:16 PM
0 Kudos
Message 4 of 7
(5,330 Views)

Hi, i have an further question about your code.

Is it possible to use one Plot for the values A1 A2 A3?


I got connected points and no Bars as i tryed to use one plot and PlotXYAppend. 

 

I have more than one sensor and i  want to draw each sensor value like the following schematic

 

A1 = Sensor 1 ADC1, Sensor 2 ADC 1

A2 = Sensor 1 ADC2, Sensor 2 ADC 2

A3 = Sensor 1 ADC3, Sensor 2 ADC 3

Message Edited by jeschki on 04-08-2010 03:44 AM
0 Kudos
Message 5 of 7
(5,156 Views)
Solution
Accepted by topic author jeschki

Hello there,

 

It seems like you want to compare different data (like A1, A2 and A3) for multiple sensors (like Sensor1 and Sensor2). I think you are looking for something like a stacked-bar-chart (see the pictures). Measurement Studio ScatterGraph has powerful APIs that can be used to implement the stacked-bar-charts.

 

I just played around the code a bit that I had sent to you to get these snaps.

 

(Picture 1)

Stacked-BarChart2.png

 

Code for (1)

using System;
using System.Drawing;
using System.Windows.Forms;
using NationalInstruments.UI;
using NationalInstruments.UI.WindowsForms;

namespace WindowsFormsApplication1
{
public partial class StackedBarChartInScatterGraphExample : Form
{
private static Color colorA1 = Color.Red;
private static Color colorA2 = Color.Blue;
private static Color colorA3 = Color.Green;

private static Pen penA1 = new Pen(colorA1, 50);
private static Pen penA2 = new Pen(colorA2, 50);
private static Pen penA3 = new Pen(colorA3, 50);

public StackedBarChartInScatterGraphExample()
{
InitializeComponent();
}

// On timer tick generate some data and plot.

private void timer1_Tick(object sender, EventArgs e)
{


GenerateDataAndPlot();
}

private void GenerateDataAndPlot()
{
// Read data from sensor1.
double dataA1 = GetDataFromSensor();
double dataA2 = GetDataFromSensor();
double dataA3 = GetDataFromSensor();

// Define X positions. All X values shoudl be same since we want them in a column.
double[] sensor1XData = new double[4] { 2, 2, 2, 2 };

// Define Y in a stacked column way.
// A1's data is from 0 to dataA1.
// A2's data is from dataA1 to (data1 + dataA2).
// A3's data is from (dataA1 + data2) to (dataA1 + dataA2 + dataA3).
double[] sensor1YData = new double[4] { 0, dataA1, dataA1 + dataA2, dataA1 + dataA2 + dataA3 };

scatterGraph1.Plots[0].PlotXY(sensor1XData, sensor1YData);

// Read data from sensor2.
dataA1 = GetDataFromSensor();
dataA2 = GetDataFromSensor();
dataA3 = GetDataFromSensor();

// Here just shift the column by giving some other value for X.
double[] sensor2XData = new double[4] { 5, 5, 5, 5 };
double[] sensor2YData = new double[4] { 0, dataA1, (dataA1 + dataA2), (dataA1 + dataA2 + dataA3)};

scatterGraph1.Plots[1].PlotXY(sensor2XData, sensor2YData);
}

Random r = new Random();
private double GetDataFromSensor()
{
// This is the place where you read data from sensor.
return r.NextDouble() * 50 + 25;
}

private void scatterGraph1_BeforeDrawPlot(object sender, BeforeDrawXYPlotEventArgs e)
{
ScatterGraph graph = sender as ScatterGraph;

if(graph != null)
{
Graphics g = e.Graphics;

// Map the data points to the Graph's plot area bouts.
// Here we get the actual x and y coordinates of the points.
PointF[] points = e.Plot.MapDataPoints(graph.PlotAreaBounds);

if (points.Length == 4)
{
// HERE WE DRAW THE PLOT OURSELVES.
g.DrawLine(penA1, points[0], points[1]);
g.DrawLine(penA2, points[1], points[2]);
g.DrawLine(penA3, points[2], points[3]);
}
}

// CANCEL THE GRAPH's PLOT DRAWING SINCE WE ARE DRAWING IT OURSELVES.
e.Cancel = true;
}
}
}

 

 

-- OR --

 

 

(Picture 2)

Stacked-BarChart3.png

 

Code for (2)

using System;
using System.Drawing;
using System.Windows.Forms;
using NationalInstruments.UI;
using NationalInstruments.UI.WindowsForms;

namespace WindowsFormsApplication1
{
public partial class StackedBarChartInScatterGraphExample : Form
{
// defines the column thickness.
private static int columnSize = 10;

private static Color colorA1 = Color.Red;
private static Color colorA2 = Color.Blue;
private static Color colorA3 = Color.Green;

private static Pen penA1 = new Pen(colorA1, columnSize);
private static Pen penA2 = new Pen(colorA2, columnSize);
private static Pen penA3 = new Pen(colorA3, columnSize);

public StackedBarChartInScatterGraphExample()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
GenerateDataAndPlot1();
}

private void GenerateDataAndPlot1()
{
// Read data from sensor1.
double dataA1 = GetDataFromSensor();
double dataA2 = GetDataFromSensor();
double dataA3 = GetDataFromSensor();

// Define X positions. All X values shoudl be same since we want them in a column.
double[] sensor1XData = new double[4] { 2, 2, 2, 2 };
double[] sensor1YData = new double[4] { 0, dataA1, dataA2, dataA3 };

scatterGraph2.Plots[0].PlotXY(sensor1XData, sensor1YData);

// Read data from sensor1.
dataA1 = GetDataFromSensor();
dataA2 = GetDataFromSensor();
dataA3 = GetDataFromSensor();

// Define X positions. All X values shoudl be same since we want them in a column.
double[] sensor2XData = new double[4] { 5, 5, 5, 5 };
double[] sensor2YData = new double[4] { 0, dataA1, dataA2, dataA3 };

scatterGraph1.Plots[1].PlotXY(sensor2XData, sensor2YData);
}

Random r = new Random();
private double GetDataFromSensor()
{
// This is the place where you read data from sensor.
return r.NextDouble() * 50 + 25;
}

private void scatterGraph1_BeforeDrawPlot(object sender, BeforeDrawXYPlotEventArgs e)
{
ScatterGraph graph = sender as ScatterGraph;

if (graph != null)
{
Graphics g = e.Graphics;

// Map the data points to the Graph's plot area bouts.
// Here we get the actual x and y coordinates of the points.
PointF[] points = e.Plot.MapDataPoints(graph.PlotAreaBounds);

if (points.Length == 4)
{
// HERE WE DRAW THE PLOT OURSELVES.

// Here we only modify the X coordinates to meet our needs.
// base point define the base line.
// value points define the top end of the lines.

// Shift the point for A1 by columnSize to left (i.e. -ve 20 pixels)
PointF basePoint1 = points[0];
basePoint1.X -= columnSize;
PointF valuePoint1 = points[1];
valuePoint1.X -= columnSize;

// Do not alter for middle column.
PointF basePoint2 = points[0];
PointF valuePoint2 = points[2];

// Shift the point for A1 by columnSize to right (i.e. +ve 20 pixels)
PointF basePoint3 = points[0];
basePoint3.X += columnSize;
PointF valuePoint3 = points[3];
valuePoint3.X += columnSize;

// Draw lines
g.DrawLine(penA1, basePoint1, valuePoint1);
g.DrawLine(penA2, basePoint2, valuePoint2);
g.DrawLine(penA3, basePoint3, valuePoint3);
}
}

// CANCEL THE GRAPH's PLOT DRAWING SINCE WE ARE DRAWING THE PLOT OURSELVES.
e.Cancel = true;
}
}
}

 

Hope this helps. If this is not what you meant, reply me.

 

 

Cheers,

Message 6 of 7
(5,130 Views)
Thank you very much for your help! The second one is exactly what I need 🙂
0 Kudos
Message 7 of 7
(5,126 Views)