Algorithm for self-scaling ruler in plotter GUI component

爱⌒轻易说出口 提交于 2019-12-12 08:57:50

问题


I am designing (not programming) a data-series viewer program for medical data, using C#/WPF. There is one main panel which shows the data, with the common mouse pan/zoom functionality (pan with left drag, horizontal zoom with scroll, vertical auto-fit with double click, selection with right drag).

The problem is: I need vertical and horizontal grid with rulers on the top/left, similar to those seen in audio-editing (goldwave, audacity), cartography (gpsTrackmaker) and ilustration (photoshop, inkscape) programs. Besides, these rulers are even configurable in most plotting APIs (Matlab, GNUPlot, Matplotlib) and even office programs (Excel, Calc).

The main requisite, besides being always aligned and scaled to content, is to have an "anti-clutter" algorithm which only draws tick labels with the maximum granularity possible without the labels "hitting" or hiding one another.

Below is an image showing two zoom levels of the same drawing on Inkscape. The first row shows each hundred labels. The second row shows each decade labels.

The question is: "Since this (the auto-resolution of viewing rulers) seems to be a solved problem in many programs, how should I look for / what are the available resouces/tutorials so I can specify and implement such a control that suits my needs?"

I would appreciate any suggestion or example, preferrably in C#, but could be in any language.


回答1:


  • W = visible protion of the ruler
  • X = range (right-left) represented by W
  • w = label width

n = W/w (maximum number of labels you can display)

x = X*w/W (minimal range between two labels)

Then you have to round x to some larger, cooler value 1, 2 or 5 in any magnitude.

For example

  • x=18.4 => x1=20
  • x=539 => x1=1000

w1 = x1/X*W (label width, or space between ticks)

n1 = W/w1 (number of ticks)



来源:https://stackoverflow.com/questions/9009265/algorithm-for-self-scaling-ruler-in-plotter-gui-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!