GithubHelp home page GithubHelp logo

vega / vega-tooltip Goto Github PK

View Code? Open in Web Editor NEW
79.0 79.0 44.0 14.02 MB

Tooltip Plugin for Vega-Lite

Home Page: http://vega.github.io/vega-tooltip/

License: BSD 3-Clause "New" or "Revised" License

JavaScript 20.76% TypeScript 72.41% Shell 0.91% SCSS 5.92%
tooltip-handler tooltip-plugin vega vega-lite

vega-tooltip's Introduction

Tooltip for Vega & Vega-Lite

npm version Build Status codecov code style: prettier

A tooltip plugin for Vega and Vega-Lite visualizations. This plugin implements a custom tooltip handler for Vega that uses custom HTML tooltips instead of the HTML title attribute. Vega Tooltip is installed in the Vega Editor.

demo image

Features

  • Renders nice tooltips for Vega and Vega-Lite charts
  • Supports dark and light theme
  • Renders object-valued tooltips as a table
  • Supports special keys title (becomes the title of the tooltip) and image (used as the url for an embedded image)

Demo

http://vega.github.io/vega-tooltip/

Installing

We recommend using Vega-Embed, which already comes with this tooltip plugin.

NPM or Yarn

Use npm install vega-tooltip or yarn add vega-tooltip.

Using Vega-tooltip with a CDN

You can import vega-tooltip directly from jsDelivr. Replace [VERSION] with the version that you want to use.

<!-- Import Vega 5 & Vega-Lite 4 (does not have to be from CDN) -->
<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@4"></script>

<script src="https://cdn.jsdelivr.net/npm/vega-tooltip@[VERSION]"></script>

Usage and APIs

If you use Vega-Embed, you don't need to install Vega Tooltip! Vega Embed already comes with Vega Tooltip. You can however pass tooltip customizations.

vegaEmbed("#vis", spec, {tooltip: {theme: 'dark'}})
  .then(function(result) {
    // result.view contains the Vega view
  })
  .catch(console.error);

If you want to use a different version of the tooltip handler, you can override the default handler with the handler from Vega Tooltip (and you need to install it separately).

var handler = new vegaTooltip.Handler();
vegaEmbed("#vis", spec, {tooltip: handler.call})
  .then(function(result) {
    // result.view contains the Vega view
  })
  .catch(console.error);

See the API documentation for details.

Tutorials

  1. Creating Your Tooltip
  2. Customizing Your Tooltip

Run Instructions

  1. In the project folder vega-tooltip, type command yarn to install dependencies.
  2. Then, type yarn start. This will build the library and start a web server.
  3. In your browser, navigate to http://localhost:8000/, where you can see various Vega-Lite and Vega visualizations with tooltip interaction.

Publishing

To make a release, run npm run release. The update the website with yarn deploy:gh.

vega-tooltip's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar domoritz avatar ericsoco avatar greenkeeper[bot] avatar hydrosquall avatar jheer avatar joelostblom avatar kanitw avatar kenklin avatar light-and-salt avatar mastergenius avatar mayagbarnes avatar melissachang avatar millette avatar nyurik avatar siddhant1 avatar sighrobot avatar silky avatar sirahd avatar svdm avatar ydlamba 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  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  avatar  avatar  avatar  avatar  avatar

Watchers

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

vega-tooltip's Issues

Write pro-tips about point and tooltip in documentation

Points, especially the enlarged ones, are not very easy to mouseover. You have to put mouse on the edge of a point to see the tooltip.
point

In this example I think it's better to use solid marks because they are easier to mouseover:
bin1

On the other hand, when marks overlap with each other, hover on edge actually works well. In the example below, hover on edge allows more marks to be picked up.
bin3point

Alias for field name

It would be good if we can allow users to specify custom names for tooltip field names. I can pass in alias in the opt object through the API

Separate fields and their format in options

Currently, the complete structure of options look like this:

var options = {
  showFields: [{ // specify data fields to be shown in the tooltip
    field: ..., // field name in the dataset
    fieldTitle: ..., // (optional) field title the tooltip should display
    type: 'date' | 'number' | 'string',
    format: timeUnit | string specifier
  }],
  offset: { // x and y offset (in pixels) of the tooltip
    x: 10,
    y: 10
  },
  colorTheme: 'light' | 'dark' // specify a color theme the tooltip should use
}

For both Vega-Lite and Vega tooltips, if you want to specify the format of a field you have to put an object like {field:..., format: ...} into options.showFields[], and if you put in an object for one field you have to put in objects for all the other fields that you want to display in the tooltip (because the tooltip plugin right now assumes you only want to display the fields in the showFields array).

It's better to separate fields and their formats. If a user wants to specify a format, she specifies the format -- it doesn't affect the "which fields get displayed" decision.

var options = {
  showFields: ['field1', 'field2', 'field3'],
  fieldsConfig: {
    field1: {
      fieldTitle: ..., // (optional) field title the tooltip should display
      type: 'time' | 'number' | 'string',
      format: timeUnit | string specifier
    }
  },
  offset: { // x and y offset (in pixels) of the tooltip
    x: 10,
    y: 10
  },
  colorTheme: 'light' | 'dark' // specify a color theme the tooltip should use
}

Use vega-lite release version

Currently we are using vega-lite.min.js from the vega-lite github page. The vega-lite library (v1.0.8) we get via npm doesn't seem to have vl.timeUnit.format

Eliminate redundancy in method call

Right now, there are way too many redundant parts in vl.tooltip and vg.tooltip
that's why you have call everything twice.

This is not a good practice for a few reasons:

  • Have to write the code twice for everything
  • If you do something wrong, then you have to correct twice.
  • It's entirely possible that you'll forget something in one of the place
  • Hard for code review.

Correctly format time.

  • take original vega-lite spec as input
  • import datalib
  • read timeUnit so we can set timeFormat
  • read config.numberFormat so we use the correct numberFormat

Package.json

  • change license to BSD like other vega projects
  • use pre 1.0 version
  • add a build script

vlTooltip default should be fieldDefs

Currently, for Vega-Lite visualizations, if no options is passed in the tooltip will show all top-level fields an item has.

But we should make it so that if no options is passed in, tooltip will only show fields that's used in the visualization, i.e. vl.spec.fieldDefs.

The spirit is that we want the user to specify as little as possible, but make the result look as good as possible.

option to show only some fields in the tooltip

  • two ways
    • add tooltip channel
    • For the tooltip API that gets call inside vg.embed or vg.parse.spec, add a third tooltip: FieldDef[] parameters to the API (1st=view, 2nd=spec) .
      • (Of course only field def that matches fields in the spec would be displayed.)
      • this mean you the spec must have all fields you want to show in tooltip already

Idea

  • This plugin becomes vg.embedVegaLiteWithTooltip(..embedparams.., tooltips, callback)

Use JS?

Since the code is <100 lines and does not leverage any typings, using typescript simply complicates things.

For small codebase like this, using only JS will have the benefit that you have the ready-to-use file in the repo without requiring complicated deploy system.

Move tooltip to vl & vg namespaces?

Right now the APIs are
vlTooltip.linkToView(view, spec, opt)
and
vgTooltip.linkToView(view, opt).

Potentially we want to move the tooltip to vl and vg namespaces (?) Then the APIs will become
vl.tooltip(view, spec, opt)
and
vg.tooltip(view, opt)

Tutorial of using `options` to customize tooltip

Currently our documentation does not contain any code example of using options to customize the tooltip. We can use the ## Options section in customizing_your_tooltip.md to do this.

The tooltip APIs are now documented in this page. So the customizing_your_tooltip.md page should be more like tutorials.

API Option

Generally we want the user to write a field name without any underscore prefixes in options.fields and options.fieldConfigs. Then the tooltip plugin supplements the field names with underscore prefixes derived from vlSpec. We want to supplement the user-provided field names in order to match the field names returned by item.datum.

For aggregated fields, user should provide field name (e.g. 'yield'). Then tooltip supplements the field name with the aggregate operation (e.g. 'mean_yield'). This should match the field names in item.datum.

One exception is the aggregate operation count, where the field name is '*'. In this case the supplemented field name should be 'count' instead of 'count_*'

For fields with timeUnit, user should provide the field name (e.g. 'date'). Tooltip supplements the field name with the timeUnit (e.g. 'month_date').

For binned fields, user should provide field name (e.g. 'Acceleration'). Tooltip supplements the field name with 'bin_' (e.g. 'bin_Acceleration'). In runtime the tooltip plugin calculates the range of the bin field (issue #29).

If user don't provide options.fields, tooltip supplements options.fields with all fieldDefs in the vlSpec, using the underscore prefixes described above.

Customize anchor point

Right now the anchor point on the tooltip is top left, so the tooltip is always at the bottom right of the mouse. It would be nice to allow users to overwrite the anchor point through options. This would make tooltip positioning more flexible.

Related: options.offset.x and options.offset.y describes the x and y distances between the mouse and the tooltip anchor point. Changing the anchor point may create some confusion around offset. For example, if anchorPoint = 'left center', should offset.x be 10px or -10px?

Customize tooltip shape?

Right now tooltip is a rectangle with 3px rounded corners. It would be nice to express more shapes (e.g. bubble?)

Mouse hover color change

Hover colors would be a nice complement to tooltip, especially when data is dense. With hover color I can actually see which mark is I'm actually looking at.

point2

bin3point

Separate into multiple files

  1. VL / VG

For Vega users, they don't need to code for vega-lite.

For Vega-Lite users, compile a version that provide all the necessary methods within one import.

  1. Modularize?

Add delay in options

options.delay can be true or false or a number. If it's false, there will be no delay. If it's true, tooltip will be delayed for 200 ms. If it's a number, tooltip will be delayed for the number of milliseconds provided.

type: date, time, or utc?

Currently, if a field in options is a temporal field, the type is date. For example:

var stackedBarOpts = {
    showFields: [
      {
        field: "month_date",
        fieldTitle: "Month",
        type: "date", // right now we use "date", but it can be "time" or "utc"
        format: "month"
      },
      {
        field: "weather",
        fieldTitle: "Weather"
      },
      {
        field: "count",
        fieldTitle: "Count"
      }
    ]
  };

Vega axes formatType uses "time" and "utc"

Test supplementOptions(options, vlSpec)

This function does not involve interaction.

The inputs and output of this function can have a lot of variations.

/**
  * (Vega-Lite only) Supplement options with vlSpec
  *
  * @param options - user-provided options
  * @param vlSpec - vega-lite spec
  * @return the vlSpec-supplemented options object
  * 
  * if options.showAllFields is true or undefined, vlSpec will supplement
  * options.fields with all fields in the spec
  * if options.showAllFields is false, vlSpec will only supplement existing fields
  * in options.fields
  */
  function supplementOptions(options, vlSpec)

Provide .on() event listeners

For example, vega-lite-ui has a need to log when tooltip appears and disappears.

vl.tooltip(vgView, vlSpec)
  .on('appear', function() { /* callback */})
  .on('disappear', function() { /* callback */})
  • update doc

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.