Reputation: 1
I am trying to build basic histogram that is located in examples of ScottPlot text. I can not manage how to join this code to my project Windows Forms App and make it work:
ScottPlot.Version.ShouldBe(4, 1, 70);
var plt = new ScottPlot.Plot(600, 400);
// create a histogram with a fixed number of bins
ScottPlot.Statistics.Histogram hist = new(min: 140, max: 220, binCount: 100);
// add random data to the histogram
Random rand = new(0);
double[] heights = ScottPlot.DataGen.RandomNormal(rand, pointCount: 1234, mean: 178.4, stdDev: 7.6);
hist.AddRange(heights);
// show the histogram counts as a bar plot
plt.AddBar(values: hist.Counts, positions: hist.Bins);
// customize the plot style
plt.YAxis.Label("Count (#)");
plt.XAxis.Label("Height (cm)");
plt.SetAxisLimits(yMin: 0);
plt.SaveFig("stats_histogram.png");
Could somebody explain how to use basic example with real Plot control? And here is my current code that i have now:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void formsPlot1_Load(object sender, EventArgs e)
{
CreateChart();
}
void CreateChart()
{
ScottPlot.Statistics.Histogram hist = new(min: 140, max: 220, binCount: 100);
double[] dataX = new double[] { 1, 2, 3, 4, 5 };
double[] dataY = new double[] { 1, 4, 9, 16, 25 };
formsPlot1.Plot.AddScatter(dataX, dataY);
formsPlot1.Refresh();
}
}
Upvotes: -1
Views: 335
Reputation: 4493
The code to make ScottPlot alive is very simple:
using ScottPlot.Plottable;
namespace nsScottPlot;
public partial class ChartSampleForm : Form {
ScatterPlot _plot1, _plot2, _plot3; Crosshair _crosshair;
public ChartSampleForm() { InitializeComponent(); } // ScottPlot.FormsPlot formsPlot1
protected override void OnLoad(EventArgs ea) {
base.OnLoad(ea);
try {
dtpStart.Value = new DateTime(2023, 12, 9, 21, 36, 0);
dtpEnd.Value = new DateTime(2023, 12, 9, 21, 40, 0);
}
catch (Exception ex) { App.ErrMsgUnexpected(ex, "Load form"); }
}
private void btnGet_Click(object sender, EventArgs e) {
try {
using var db = new MyContext();
var xa = db.ParameterSets.Where(w => w.Timestamp >= dtpStart.Value && w.Timestamp <= dtpEnd.Value)
.Select(s => System.Convert.ToDouble(s.Timestamp.ToOADate())).ToArray();
_plot1 = formsPlot1.Plot.AddScatter(xa, db.ParameterSets.Where(w => w.Timestamp >= dtpStart.Value && w.Timestamp <= dtpEnd.Value)
.Select(s => (double)s.Tag1).ToArray());
_plot2 = formsPlot1.Plot.AddScatter(xa, db.ParameterSets.Where(w => w.Timestamp >= dtpStart.Value && w.Timestamp <= dtpEnd.Value)
.Select(s => (double)s.Tag2).ToArray());
_plot3 = formsPlot1.Plot.AddScatter(xa, db.ParameterSets.Where(w => w.Timestamp >= dtpStart.Value && w.Timestamp <= dtpEnd.Value)
.Select(s => (double)s.Tag3).ToArray());
var plt = formsPlot1.Plot;
plt.XAxis.DateTimeFormat(true);
_crosshair = plt.AddCrosshair(xa[0], 0); _crosshair.HorizontalLine.IsVisible = false;
_crosshair.VerticalLine.PositionFormatter = x => DateTime.FromOADate(x).ToString("T");
plt.Title("Flowrate"); plt.YLabel("bbl/d"); formsPlot1.Refresh();
formsPlot1.MouseMove += formsPlot1_MouseMove;
}
catch (Exception ex) { App.ErrorTraceEx(ex, "Get chart"); }
}
private void formsPlot1_MouseMove(object? sender, MouseEventArgs e) {
(double mouseCoordX, _) = formsPlot1.GetMouseCoordinates();
(_, double pointY, _) = _plot1.GetPointNearestX(mouseCoordX); label8.Text = pointY.ToString("N1");
(_, pointY, _) = _plot2.GetPointNearestX(mouseCoordX); label7.Text = pointY.ToString("N1");
(_, pointY, _) = _plot3.GetPointNearestX(mouseCoordX); label6.Text = pointY.ToString("N1");
_crosshair.X = mouseCoordX; formsPlot1.Refresh(lowQuality: true, skipIfCurrentlyRendering: true);
}
}
Upvotes: -1