Git Product home page Git Product logo

plot-grid's Introduction

plot-grid unstable gzip-size

Grid component for webgl/canvas2d with zooming, panning, polar mode etc. See demo.

Usage

npm install plot-grid

const createGrid = require('plot-grid');

//cartesian grid
let grid = createGrid({
	x: {
		type: 'logarithmic',
		min: 0
	},
	y: {
		min: -100,
		max: 0
	}
});

This will create frequency response and directional diagram.

API

let grid = new Grid(options?)

Create new grid instance. It can serve both as a class or constructor function (no new). By default it displays cartesian grid, but

Name Default Description
container document.body Container to place grid into. Can be null to render in memory.
context null Can be pre-existing context.
pixelRatio window.devicePixelRatio Pixel ratio of canvas.
autostart true Render every frame automatically or call render method manually. Useful if plot-grid is used in cooperation with other components.
interactions true Enable pan/zoom interactions, see interact event.
x, y, r, a Bool, String, Object Boolean, enabling coordinates of linear type or a string, defining custom type: linear, logarithmic or time. If object is passed, it defines custom lines behaviour, see the table below.

Each of x, y, r, a can be customized by the following options:

Name Type Default Description
type String, null null Default type to extend, one of linear, logarithmic, time.
color String rgba(0,0,0,1) Default color for the lines, axes, ticks and labels.
format Function null Formatter for label values. Takes a value and returns a string. pretty-number can be used as such.
lines Bool, Array, Function, null Array with values, defining lines, or function returning such array, state => [values...]. Can be disabled by passing false. By default implemented by type.
ticks Bool, Array, Number, Function 5 Tick size. Can be disabled by passing false.
labels Bool, Array, Object , Function, null null Object or array with labels corresponding to lines. Can be defined as a function returning such array (state) => labels. null value will output values as is. Can be disabled by passing false.
axis Bool true Enable axis.

Pan & zoom

Additional pan/zoom params can be set for each coordinate x, y, r, a:

Name Type Default Description
offset Number 0 Defines start point for the visible range, in terms of values.
origin Number 0.5 Defines position of the offset on the screen, for example, .5 for center, 1 for right/top edge of the screen, 0 for left/bottom.
scale Number 1 Sets scale for the current range, number of values per pixel.
min, max Number -Infinity, Infinity Limits for panning.
minScale, maxScale Number 0, Infinity Scale limits.
zoom Bool true Enables zoom interaction.
pan Bool true Enables pan interaction.

To change pan or zoom, use update method with the propertives above, as update({x: {offset, scale}, y: {offset, scale});.

Another time it might be useful to engage grid.on('interact', grid => {}) handler for grid interactions, like moving and zooming.

Style

Each coordinate can be customized more with additional options:

Name Type Default Description
lineColor String, Number, Function, null .3 Color for lines. Number value will take the base color above with changed opacity. Function signature is state => [...values].
lineWidth Number 1 Width of lines. We guess that width of sublines should not differ from the width of lines, if you have use-case requiring the opposite, please address issues.
axisOrigin Number 0 Define axis alignment by value on the opposite coordinate.
axisColor String, Number 0.1 Axis color, redefines default color.
axisWidth Number 2 Width of axis line.
align Number 0.5 The side to align ticks and labels, 0..1.
fontSize String, Number 10pt Font size for labels. Sizes with units will be automatically transformed to pixels by to-px.
fontFamily String sans-serif Font family to use for labels.
padding WIP Number, Array(4) 0 Padding inside the viewport to indent lines from axes and labels. Ordering is top, right, bottom, left, as in css.
style WIP String lines Style of rendering: lines or dots. Note that dots is available only when x and y are both enabled.
distance Number 120 Minimum distance between lines.

grid.update(options)

Pass new options to update grid look. Note that passed options extend existing ones.

grid.update({
	x: {
		type: 'logarithmic',
		offset: 0,
		min: 0,
		scale: .01
	}
});

Note that you may need to call render in manual mode grid.update().render().

grid.render()

Redraw grid. Call whenever you need to redraw grid, like resize etc. It will not recalculate lines, just rerender existing lines. To recalculate lines, use grid.update().

grid.draw()

Directly invoke draw method, useful in case if grid needs to be drawn over other content.

Used by

Thanks

To @evanw with thetamath for grid API inspiration.

Related

  • grid — collection of grids for canvas2d.

plot-grid's People

Contributors

dy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

plot-grid's Issues

2.0 todo

Current version seems to be insufficient due to the set of shortages.

  • pan method, for faster reorientation, like time shift in gl-waveform. update is too large for that.
  • zooming with auto(re)layout of lines, like thetamath
  • put axes with labels to a separate element, to easily adjust position for canvas grid no more actual
  • adjustable position of axes, the value
  • symmetrical mode for log spanning over 0 use linear mode with custom lines
  • style options, like color of lines/axes
  • dots style of rendering
  • webgl version
  • log grid fixes
    • show 2/5 major lines in middle scales
      • with subdivisions by 1/2/5
    • limit range for very small scales, do not span over decade, it is too slow
    • add proper decibel lines placement use linear mode with custom lines placement
    • display units, with proper SI prefix, which could allow for avoiding 10² for a while use si-tools, human-format or other lib to format SI, place in format method
    • group large scales as 1e0 1e+5 1e+10 ..., i. e. 1/2/5
    • fix handing bug for very large scale, do clamping
    • fix formatting of digits, ideally 10¹, 10², ... pretty-number can be used as such external package
    • scale 10⁰ 10² 10⁴ has intermediate values of 10^1.5, ..., which have no sense
  • add type property, setting up lines type from scratch: log, linear, time
  • update pan-zoom to handle single callback
  • test negative directions, specifically negative log
  • add polar mode
  • fix clipping axis labels placement bug
  • expand time type to Infinity
  • API iteration, see below

Firefox webgl mode

Is terribly slow, esp. while zooming.
thetamath is terribly good for that btw.

gl-line module

Vertical lines are unlucky case of antialiasing. To make them render properly we gotta follow the practices described here. Anyways as far as we are going to use canvas2d for rendering labels instead text-cache, for now we use "hacky" fully 2d-rendering to texture. Later on, when there are enough use-cases, let’s switch to simple gl-line2d renderer.

Bring more stable steps to dictaphone

Copying iphone’s 1, 2.5, 5 steps is maybe not the best solution for large scales, like 2.5s step — whaat? Why not 2s? I guess it is better stick to 1, 2, 5 more persistently, as it works with hours, minutes and other dimensions ok. And for big labels keep 1 and 5, because 2 is not really a major step.

Padding

Make padding work properly

Speed up firefox, IE

Especially FF.
I guess before update I should add 'hidden' or something.
Anyways it is not urgent, when real use-cases will require that.

log < 1

logarithmic range spanning over 1 places lines unevenly

Canvas mode

For dynamic draw, like shifting grid, there is no sense in keeping hover mode, we can replace html with single canvas, it is going to be faster.
See gl-waveform use case.

Webgl text rendering

Now labels in webgl mode are just span elements.
That drops FPS in firefox and iphone.
We could try doing some cache-texture in canvas2d and forward it to webgl whenever cache changed.
Worst case it is going to be like gl2d.
Best case it will avoid moving texts at all.
That should be a separate component, like gl-render-text, but without texture2d wrapper as an output - just simple renderer.

Save to svg

Related to the old idea of notebooks prints, this grid, carried to the end would allow for printing custom notebook grid

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.