Code Monkey home page Code Monkey logo

vue.d3.sunburst's Introduction

Vue.D3.Sunburst

GitHub open issues Npm version MIT License

demo gif

Live example:

https://david-desmaisons.github.io/Vue.D3.sunburst/

About

Vue.D3.Sunburst provides a reusable vue sunburst charts component relying on D3.js.

Sunburst is the main component of this library and display sunburst chart based on data props using children property for hierarchy, name property for naming and size property for arcs size.

Sunburst component provides four optional slots:

  • legend and top slots are intended to be used to display additional information such as legend
  • pop-up to create a pop-up for an arc
  • default slot is intended to receive renderless component providing behavior: for example, highlighting on mouse over or zoom on click

Components providing standard and reusable features are provided.

Features

  • Reactive to data changes
  • Responsive to size changes
  • Customizable with slot or exposed methods and events
  • Zoomable

Install

npm i vue-d3-sunburst

Usage

<sunburst :data="data">

  <!-- Add behaviors -->
  <template slot-scope="{ on, actions }">
    <highlightOnHover v-bind="{ on, actions }"/>
    <zoomOnClick v-bind="{ on, actions }"/>
    <popUpOnHover v-bind="{ on, actions }"/>
  </template>

  <!-- Add information to be displayed on top the graph -->
  <nodeInfoDisplayer slot="top" slot-scope="{ nodes }" :current="nodes.mouseOver" :root="nodes.root" description="of visits begin with this sequence of pages" />

  <!-- Add bottom legend -->
  <breadcrumbTrail slot="legend" slot-scope="{ nodes, colorGetter, width }" :current="nodes.mouseOver" :root="nodes.root" :colorGetter="colorGetter" :from="nodes.clicked" :width="width" />

   <!-- Add pop-up -->
   <template slot="pop-up" slot-scope="{ data }">
      <div class="pop-up">{{data.name}}</div>
   </template>

</sunburst>
import {
  breadcrumbTrail,
  highlightOnHover,
  nodeInfoDisplayer,
  popUpOnHover,
  sunburst,
  zoomOnClick
} from 'vue-d3-sunburst';
import "vue-d3-sunburst/dist/vue-d3-sunburst.css";

export default {
  components: {
    breadcrumbTrail,
    highlightOnHover,
    nodeInfoDisplayer,
    sunburst,
    zoomOnClick
  },
  data() {
    return { 
      tree:  {
      "name": "flare",
        "children": [
          {
            "name": "analytics",
            "children": [
              {
                "name": "cluster",
                "children": [
                  { "name": "AgglomerativeCluster", "size": 3938 },
                  { "name": "CommunityStructure", "size": 3812 },
                  { "name": "HierarchicalCluster", "size": 6714 },
                  { "name": "MergeEdge", "size": 743 }
                ]
              },
              {
                "name": "graph",
                "children": [
                  { "name": "BetweennessCentrality", "size": 3534 },
                  { "name": "LinkDistance", "size": 5731 },
                  { "name": "MaxFlowMinCut", "size": 7840 },
                  { "name": "ShortestPaths", "size": 5914 },
                  { "name": "SpanningTree", "size": 3416 }
                ]
              },
              {
                "name": "optimization",
                "children": [
                  { "name": "AspectRatioBanker", "size": 7074 }
                ]
              }
            ]
          }
        ]
      }
    }
  }
}

Gotchas

This component is responsive and will adjust to resizing. In order for this to work properly, you must define for this component or its parent wether:

  • a height or a max-height
  • or a width or a max-width.

Failing to do so may result in a component whose size that will keep increasing.

API

sunburst

slots

  • legend Use this slot to add information on top or bottom of the graph

  • top Use this slot to add information on top of the graph

  • pop-up Use this slot to create a pop-up for arc: it receives as scope data, node and actions objects

  • default Use this slot to add behaviors to the sunburst

props

  • data Object (optional)

    Sunburst data where children property is a array containing children.

  • color-scheme String (optional) default: provided by injected defaultSchemeColor with a fall back to schemeAccent

    D3 color scheme to be used.

  • color-scale Function (optional) default: undefined

    D3 scale of colors (d3-scale). If specified, it overwrites the color-scheme props

  • get-category-for-color Function (optional) default: 'useNameForColor'

    Function used to map an item and its color. (nodeD3: Object) => category: Number | String By default use the node name

  • highlightOpacity Number (optional) default: '0.3'

    Opacity to be applied to none highlighted nodes

  • min-angle-displayed Number (optional) default: 0.005

    Minimal arc angle to be displayed (in radian).

  • arc-identification Function (optional) default: 'recursiveName'

    Function used to identify an arc, will be used during graph updates. (nodeD3: Object) => id: String

  • in-animation-duration Number (optional) default: 100

    Duration for in animation in milliseconds

  • out-animation-duration Number (optional) default: 1000

    Duration for out animation in milliseconds

  • show-labels Boolean or Function (optional) default: false

    • If true displays the name attributes as arc labels
    • If the value is a function, it must return the text that will be display for each arc. The function is called with an object similar to the D3.js node with the addition of a context property: { context: {angle: 45, relativeDepth: 1} } where angle is the arc angle in degree and relativeDepth is the arc depth in the current display (taking into account zoom). To display nothing returns null.
  • max-label-text Number (optional) default: 45

    Useful to truncate labels: the maximum size of a label is the corresponding arc size + the value of max-label-text in pixel. If max-label-text is null, the labels will not be truncated.

  • central-circle-relative-size Number (optional) default: 0

    Size in percentage of the total graph, of a central circle

events

  • mouseLeave

    Fired when mouse leaves the sunburst node.

  • rootChanged

    Fired when root changed.

    arguments:

    • value Object - {node, sunburst} The corresponding node and sunburst component
  • mouseOverNode

    Fired when mouse is over a sunburst node.

    arguments:

    • value Object - {node, sunburst} The corresponding node and sunburst component
  • clickNode

    Fired when sunburst node is clicked.

    arguments:

    • value Object - {node, sunburst} The corresponding node and sunburst component
  • zoomedChanged

    Fired when the zoomed node changed.

    arguments:

    • value Object - {node, sunburst} The corresponding zoomed node and sunburst component
  • highlightedChanged

    Fired when mouse is over a sunburst node.

    arguments:

    • value Object - {node, sunburst} The corresponding node and sunburst component
  • resize

    Fired when component is resizing.

    arguments:

    • value Object - {radius, height, width} Size information in pixel

methods

  • highlightPath(node, opacity)

    Highlight the arc path leading to a given node.

    parameters:

    • node Object - the D3 node to highlight
    • opacity Number - opacity of the none highlighted nodes, default to highlightOpacity
  • zoomToNode(node)

    Zoom to a given node.

    parameters:

    • node Object - the D3 node to zoom to.
  • resetHighlight()

    Reset the highlighted path

Other optional components provided as slot implementation

Besides sunburst component, Vue.D3.Sunburst provides additional optional components that can be used out of the box as slot implementations. There are two kinds of additional components:

  • the "renderfull" components provide visual additional visual information on the nodes. They can be used as legend or top slots.
  • the renderless components provide predefined behaviors for the sunburst components. They can be used as default slots. It is possible to combine behavior using a root template slot element as in the example.

"Renderfull" components

breadcrumbTrail

Breadcrumb trail component displaying path between root node and current node. Can be used as a legend slot of sunburst component.

props
  • root Object (optional)

    Root node

  • current Object (optional)

    Current node

  • from Object (optional)

    Reference node, parents nodes of the current will have an opacity below 1

  • color-getter Function (required)

    ColorGetter exposed by sunburst

  • width Number (optional)

    Sunburst width

  • order Number (optional) default: 1

    Css Order. If 1 the slot is displayed below the sunburst, if 0 the slot is displayed on top the sunburst

  • item-width Number (optional) default: 80

    Bread crumb item width

  • item-height Number (optional) default: 30

    Bread crumb item height

  • spacing Number (optional) default: 3

    Spacing between breadcrumb items

  • tail-width Number (optional) default: 10

    With of tailing element

nodeInfoDisplayer

Component that display the percentage value of the current node relative to root. Can be used as a "top" slot of sunburst component.

props
  • root Object (optional)

    Root node

  • current Object (optional)

    Current node

  • clicked Object (optional)

    Clicked node

  • description String (required)

    Text to be displayed

  • show-all-number Boolean (optional) default: true

    Show fraction format of size if true

computed properties
  • percentage

    dependencies: current, root, current, base

  • displayPercentage

    dependencies: percentage

  • show

    dependencies: percentage

Behavioral

These components can be used as a default slot of the sunburst component which received as attributes:

  • on which is the $on method of the sunburst component:

  • actions which is an object containing the attributes:

    • highlightPath: function that takes a node and highlight the path going from the root to the given node
    • zoomToNode: function that takes a node and zoom to the corresponding node
    • resetHighlight: function that resets the highlighting
    • setContextMenu: function that takes a node and display the corresponding pop-up
    • closeContextMenu: function that remove the current pop-up if any

Typical usage:

   this.on("clickNode", ({node}) =>{
      this.actions.zoomToNode(node);
    });

zoomOnClick

Renderless component providing the zoom on click behavior. Can be used as a default slot of sunburst component.

props
  • on Function (required)

    Sunburst event listener. Same as component $on method.

  • actions Object (required)

    Sunburst actions. Typically provided by sunburst default slot.

highlightOnHover

Renderless component providing path highlighting on mouse over behavior. Can be used as a default slot of sunburst component.

props
  • on Function (required)

    Sunburst event listener. Same as component $on method.

  • actions Object (required)

    Sunburst nodes. Typically provided by sunburst default slot.

popUpOnHover

Renderless component displaying arc pop-up on mouse over. Can be used as a default slot of sunburst component.

props
  • on Function (required)

    Sunburst event listener. Same as component $on method.

  • actions Object (required)

    Sunburst nodes. Typically provided by sunburst default slot.

Installation

npm install vue-d3-sunburst

Project setup

npm install

Compiles and hot-reloads for development

npm run serve

Compiles and minifies for production

npm run build

Lints and fixes files

npm run lint

Run your unit tests

npm run test:unit

Update the API section of README.md with generated documentation

npm run doc:build

vue.d3.sunburst's People

Contributors

david-desmaisons avatar erikbjare avatar matthieucov avatar mnowik 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

Watchers

 avatar  avatar  avatar

vue.d3.sunburst's Issues

setup / install

the component looks great, but I'm having a bit of trouble getting started. Not specific to your sunburst code, but just how to use this external component in a vue project, hope you don't mind me asking here.

I have added the component to package.json in client side of my app.
but i get this module not found error

ERROR in ./src/pages/TreePage.vue?vue&type=script&lang=js& (./node_modules/babel-loader/lib!./node_modules/vue-loader/lib??vue-loader-options!./src/pages/TreePage.vue?vue&type=script&lang=js&)
Module not found: Error: Can't resolve 'vue-d3-sunburst' in '/Users/dc/dev/rikai/nap/web-app/client/src/pages'
 @ ./src/pages/TreePage.vue?vue&type=script&lang=js& (./node_modules/babel-loader/lib!./node_modules/vue-loader/lib??vue-loader-options!./src/pages/TreePage.vue?vue&type=script&lang=js&) 53:0-110 57:21-36 58:22-38 59:23-40 60:14-22 61:17-28

I'm using a build script and babel to transofrm

Is there some other webpack or other configuration that I need to setup?

Colour schemes and colour blocking children

Is there a way to pass down the same colour from parent data to children data so that children data is organised into colour blocks that relate directly to their parent?

This is possible with the D3 Zoomable Sunburst, as seen in this example:

Screenshot 2019-04-09 at 16 38 10

Also, is it possible to create customised colour schemes? Thanks!

Label rotation

Hi,

Thanks for writing this very useful library.

I've noticed that in the plots I generate that the labels are often so long that they overlap each other on consecutive levels e.g.
image

I couldn't find it in the API, is there a way to rotate the labels 90 degrees? For instance, like in this krona diagram
image

If this is not part of the API, and you don't have time to implement that, I'm curious what strategy you would take to modify the code to enable that functionality? Specifically, is there a function that could be exposed?

Also how can we zoom out once a segment has been clicked? I couldn't work out how to do this on the example page.

And one small fix - I think the <sunburst :data="data"> in the Usage section should be <sunburst :data="tree">.

Thanks in advance, ben

color-scheme option broken

First off: Thanks for making this, using it has been a really smooth experience!

Setting any color scheme other than the default seems to mess up the coloring.

I first noticed it in the demo, works fine until you switch to any other scheme. Here's a screenshot:

image

I replicated it in the thing I'm building as well, would be nice to have it working (I might hunt it down myself, but don't hold your breath).

Use size of parent nodes if present

Hey, first, thanks for that wrapper!

I have currently an issue where due to items that can have multiple categories, the total count gets calculated wrongly. Not a big issue as I have the correct number, but at the moment, they get ignored, even as they are there. Is there a way to overwrite the calculated numbers by the real ones?

grafik

Data:
grafik

Vue 3 Support?

Is there any plans to add support to the new version of Vue? It gives a few minors errors when trying to use it with the new version and the component does not compile. Such as:

vue-d3-sunburst.umd.js?6987:13880 Uncaught (in promise) TypeError: Cannot read properties of null (reading 'defaultSchemeColor')

Because of this prop:

colorScheme: { type: String, required: false, default: function _default() { return this.defaultSchemeColor; }, validator: function validator(value) { return Object.keys(colorSchemes).indexOf(value) !== -1; } }

Scaling issues

As might be seen in the attached image I'm experiencing some scaling issues with your component.

screen shot 2018-09-23 at 17 10 12

screen shot 2018-09-23 at 17 15 30

I have tried to fix the issue with various css rules, but this has no effect on the size of the diagram only the element containing it.

How to I modify the width of the individual elements of the breadcrum and how do I scale up the diagram?

Updating the chart with new data fails when zoomed in

Hi, I set up a simple example on CodeSandbox.

The problem is that after zooming in a certain slice and reassigning the data the chart sometimes disappears completely:

vue-d3-reassign-bug

But what's interesting is that not all slices will cause the bug:

no-bug

Does anyone have any idea what might be the cause? I'm also trying to fix the issue myself, but unfortunately I'm quite new to the code in this repository.

Apply color to each node through data

Is it possible to use get-category-for-color to apply a colour to each node that is passed in through the data object and if yes, how? For example when the data object looks like this:

data: {
  name: 'base',
  children: [
    {
      name: 'Fruity',
      color: '#da1f24',
      children: [
        {
          name: 'Berry',
          color: '#de4b52',
          children: [
            {
              name: 'Raspberry',
              color: '#e52a68',
              size: 1,
            },
            {
              name: 'Blueberry',
              color: '#6469af',
              size: 1,
            },
          ]
        },
        {
          name: 'Dried fruit',
          color: '#ca4a44',
          children: [
            {
              name: 'Raisin',
              color: '#b43b54',
              size: 1,
            },
          ]
        },
        {
          name: 'Citrus fruit',
          color: '#f7a129',
          children: [
            {
              name: 'Grapefruit',
              color: '#f26354',
              size: 1,
            },
            {
              name: 'Orange',
              color: '#e1641f',
              size: 1,
            },
            {
              name: 'Lemon',
              color: '#fde404',
              size: 1,
            },
          ]
        },
      ]
    },
    {
      name: 'Sour/Fermented',
      color: '#ebb20f',
      children: [
        {
          name: 'Sour',
          color: '#e1c217',
          children: [
            {
              name: 'Alcohol/Fermented',
              color: '#9fa81a',
              size: 1,
            },
            {
              name: 'Citric acid',
              color: '#f9ee01',
              size: 1,
            },
          ]
        },
        {
          name: 'Alcohol/Fermented',
          color: '#b09832',
          children: [
            {
              name: 'Winey',
              color: '#8f1a52',
              size: 1,
            },
            {
              name: 'Whiskey',
              color: '#b34038',
              size: 1,
            },
          ]
        }
      ]
    },
  ]
},

Minimal arc angle to be displayed when Zoomed in

Every time a section is zoomed in, all their children increase in area. Therefore, their angles also become bigger. Nevertheless, the "Minimal arc angle" information doesn't seem to be recomputed, as subsequent children, which became bigger than it, should be displayed and are not.

function getDx() determines the wrong dx for datalabels with 180 degrees

First of all, thank you for this great chart. I really appreciate your work!

Recently I encountered a small problem:
when a dataLabel is exactly 180 degrees, the dx is 5 and the dataLabel falls into the middle of the chart.
Bildschirmfoto 2022-11-30 um 14 41 14
Probably this will fix the problem (textAngle < 180 instead of textAngle <= 180):
function getDx({ textAngle, currentDx }) { if (textAngle < 180) { return 5; } return !!currentDx && currentDx < -5 ? currentDx : -5; }
What do you think?

Labeling arcs

I'm building ActivityWatch and we're using this lib for our sunburst visualization of time spent per category. (again, thanks!)

We've implemented the hover feature to show information about the represented data, but it's hard to get an overview at a glance, which makes me want to implement labeling of the arcs similar to what I've had in some of my Python visualizations:

image

I'm not super skilled at D3, but I'm just posting it here in case it piques someone's attention/interest, and to keep myself accountable in case I drop the ball.

Something akin to this: http://bl.ocks.org/metmajer/5480307

I found this for creating rounded text (might not be the best way to go, but still): https://www.visualcinnamon.com/2015/09/placing-text-on-arcs.html

Formatar resultados | Format Results

Olá, tudo bom ?Gostaria de saber se é possível formatar os valores apresentados no gráfico. (R$ 500.00 / R$15.00,00)
Hello, how are you? I would like to know if it is possible to format the values ​​shown in the graph. (R$500.00 / R$15,000.00)
image

Breadcrumb width

Hi there, great component! Is it possible to set the width of the breadcrumbs to accomodate longer text?

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.