ni.com is currently undergoing scheduled maintenance.
Some services may be unavailable at this time. Please contact us for help or try again later.
03-02-2010 01:52 AM
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?
Solved! Go to Solution.
03-03-2010 04:35 AM - edited 03-03-2010 04:43 AM
Hello there,
It is possible to display data as BarChart in ScatterGraph. This is how you can do it.
I have attached a sample application to demonstrating the same.
Vijet Patankar
National Instruments03-05-2010 03:05 AM
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.
03-07-2010 09:15 PM - edited 03-07-2010 09:16 PM
04-08-2010 03:36 AM - edited 04-08-2010 03:44 AM
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
04-09-2010 03:52 AM
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)
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)
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,
04-09-2010 04:42 AM