ScottPlot / ScottPlot

Interactive plotting library for .NET
https://ScottPlot.net
MIT License
5.21k stars 848 forks source link

How to get data area size in pixels (WinForms) #4332

Open jwwq opened 5 days ago

jwwq commented 5 days ago

I want to make the width of the bar in barplot constant in pixels regardless of the chart size, so here is the pseudocode:

var bar_width = 10;
var num_of_bars = [CHART_DATA_AREA_X_SIZE] / bar_width; 
p.Plot.Axes.SetLimitsX(0, num_of_bars);

How can I get the size of the "data area" (excluding axes etc) in pixels? Thanks!

P.S. If there's a more elegant way to accomplish this, please let me know.

StendProg commented 5 days ago

Hi @jwwq,

Try this p.Plot.LastRender.DataRect.Width This contains the size from the last render, which may not always be true for the current render as well.

The correct way would be to define your own Plottable inherited from BarPlot. Override the Render() method and implement any logic in it. On input it gets a RenderPack which contains information about the current size of the DataRect.