private void OnDataAvailable(object sender, NAudio.Wave.WaveInEventArgs args) { int bytesPerSample = wvin.WaveFormat.BitsPerSample / 8; int samplesRecorded = args.BytesRecorded / bytesPerSample; Int16[] lastBuffer = new Int16[samplesRecorded]; for (int i = 0; i < samplesRecorded; i++) { lastBuffer[i] = BitConverter.ToInt16(args.Buffer, i * bytesPerSample); } int lastBufferAmplitude = lastBuffer.Max() - lastBuffer.Min(); double amplitude = (double)lastBufferAmplitude / Math.Pow(2, wvin.WaveFormat.BitsPerSample); if (amplitude > peakAmplitudeSeen) { peakAmplitudeSeen = amplitude; } amplitude = amplitude / peakAmplitudeSeen * 100; buffersRead += 1; // TODO: make this sane ScottPlot.PlottableAxLine axLine = (ScottPlot.PlottableAxLine)scottPlotUC1.plt.GetPlottables()[1]; axLine.position = (buffersRead % amplitudes.Length) * 20.0 / 1000.0; Console.WriteLine(string.Format("Buffer {0:000} amplitude: {1:00.00}%", buffersRead, amplitude)); PlotAddPoint(amplitude); }
private void GenerateNewData() { Random rand = new Random(); int pointCount = 100; ScottPlot.OHLC[] ohlcs = ScottPlot.DataGen.RandomStockPrices(rand, pointCount); double[] volumes = ScottPlot.DataGen.Random(rand, pointCount, 500, 1000); double[] timestamps = new double[ohlcs.Length]; for (int i = 0; i < timestamps.Length; i++) { timestamps[i] = ohlcs[i].time; } // stock price chart formsPlot1.plt.Clear(); formsPlot1.plt.YLabel("Share Price"); formsPlot1.plt.Title("ScottPlot Candlestick Demo"); plottedLine = formsPlot1.plt.PlotVLine(timestamps[0], color: Color.Gray, lineStyle: ScottPlot.LineStyle.Dash); plottedLine.visible = false; if (rbCandle.Checked) { plottedOHLCs = formsPlot1.plt.PlotCandlestick(ohlcs); } else { plottedOHLCs = formsPlot1.plt.PlotOHLC(ohlcs); } plottedText = formsPlot1.plt.PlotText("", timestamps[0], ohlcs[0].low, bold: true, fontSize: 10, color: Color.Black, frame: true, frameColor: Color.DarkGray); formsPlot1.plt.Ticks(dateTimeX: true); formsPlot1.plt.AxisAuto(); // volume chart formsPlot2.plt.Clear(); formsPlot2.plt.YLabel("Volume"); formsPlot2.plt.PlotBar(timestamps, volumes, barWidth: .5); formsPlot2.plt.AxisAuto(.01, .1); formsPlot2.plt.Axis(null, null, 0, null); formsPlot2.plt.Ticks(dateTimeX: true); formsPlot1.plt.MatchLayout(formsPlot2.plt, horizontal: true, vertical: false); formsPlot1.Render(); formsPlot2.Render(); }