1.0.14 • Published 14 days ago

emerald-plotly-react v1.0.14

Weekly downloads
-
License
MIT
Repository
github
Last release
14 days ago

emerald-plotly-react

emerald-plotly-react is an extension to the renowned data visualization library, Plotly. Our module is designed to enhance and simplify your data visualization experience by offering additional functionality that seamlessly integrates with Plotly's core features.

Screenshot

Key Features

  • Separation of concerns: We've innovated the way you handle plot specifications and data transformations. With emerald-plotly-react, you can independently define what to plot and how to plot it, streamlining the process of creating complex visualizations with multiple subplots.
  • Flexible layout: Customize your plot layouts with ease. Our enhanced layout configurations offering additional flexibility, giving you more control over your visualization's appearance.
  • Zoom management: Additional zoom functionality.
  • Subplot editor: Allowing to plot multiple subplots with an editor based on json-editor that makes it possible to edit the subplot traces on the fly.
  • Customisable subplot menu: Each subplot has a custom menu which you can add as many custom items you want to.

Installation guide

Installing emerald-plotly-react is straightforward using npm. Here's how you can add it to your project:

Navigate to your project directory

  • Use the cd command to go to the directory where you want to install emerald-plotly-react.

Install

npm install emerald-plotly-react

Verify the Installation

  • After installation, you can check your node_modules folder to see if emerald-plotly-react and its dependencies have been installed correctly.

Getting Started

Here is a full Example Project

To help you get started with emerald-plotly-react, here's an example of how you can use the BasePlot component within a React application:

import React, { useState } from "react";
import { BasePlot } from "emerald-plotly-react";

const element_radtemp = {
  fn: function (context, args) {
    return [
      {
        type: "scattergl",
        x: context.radial.dist,
        y: context.radial.temp,
      },
    ];
  },
  xaxis: "dist",
  yaxis: "temp",
  schema: (context) => {
    // Json-schema for `args` parameter to `fn` above, used to generate a form in the edit menu.
    return {
      type: "object",
      required: [],
      properties: {},
      additionalProperties: false,
    };
  },
};

export const elements = {
  traces: { "Radial temperature": element_radtemp },
  xaxis: {
    // Units used on x-axes
    dist: {
      title: { text: "distance (m)" },
    },
  },
  yaxis: {
    // Units used on y-axes
    temp: {
      title: {
        text: "temperature (deg c)",
      },
    },
  },
};

const initial_layout = {
  layout: {
    grid: {
      columns: 2,
      pattern: "independent",
      roworder: "bottom to top",
      rows: 2,
      subplots: [
        ["xy", "x2y2"],
        ["x3y3", "x4y4"],
      ],
      xgap: 0,
      ygap: 0.0,
    },
  },
  traces: [{ "Radial temperature": { xaxis: "x", yaxis: "y" } }],
};

const PlotContainer = () => {
  const [plotLayout, setPlotLayout] = useState(initial_layout);

  // Data that can be plotted; typically you'd use typed arrays here
  let context = {
    radial: {
      dist: [1, 2, 3],
      temp: [30.2, 31.3, 30.9],
    },
  };

  return (
    <div style={{ width: "100%", height: "100%" }}>
      <BasePlot
        context={context}
        plot={structuredClone(plotLayout)} // Deep copy of plot layout to avoid direct state mutation
        setPlot={setPlotLayout}
        elements={elements}
      />
    </div>
  );
};

export default PlotContainer;

Architecture

A plot layout consists of plot a set of subplots, each subplot holding any number of plot elements. Each plot element (e.g. element_radtemp in the example above) can be used multiple times in the same subplot with different parameters. Data used by the plot elements is stored in a context object. Each plot element has a specific unit of measure for their x and y axis, and only plot elements that share the same units for their axes can be plotted in the same subplot (or, if subplots share axes, in subplots that share that axis).

Plot elements

A plot element defines how to plot some data. It consist of an object with a few required (and some optional) properties and methods: xaxis, yaxis, fn, schema. xaxis and yaxis give the units of measure for the two axes (note: not the actual axes of whatever subplot this is used in). fn generates a list of plotly traces to be plotted given a context object and optional arguments. schema finally gives a JSON-schema for the optional arguments of fn.

Plot context

The plot context is an object that holds all data that can be plotted. No particular structure is enforced for the plot context object. It is available to all plot element methods.

Units

As mentioned above, plot elements need to specify the units of measure of the two axes. No special syntax is enforced for this, and you can just make up names. They are matched against the unit names of other elements the user tries to add to the same subplot, and only elements with the same units are allowed to share the same axis. Additionally, plotly axis configuration (such as text styling, title, tick marks etc) can be applied to each unit.

Layouts

A layout is an object that specifies a plotly layout and a set of plot element instances to plot. The layout can be modified by the use using the cogwheel menu on each subplot and colorbar. It therefore needs to live in a react state, and you can easily implement save/load functionality for the user as it is a pure JS object that can be serialized to JSON.


BasePlot Component API Reference

The BasePlot component is a wrapper around Plotly's Plot component, integrating custom subplot management, zoom control, and dynamic plot updating.

KeyDescriptionTypeDefaultRequired
contextThe context object containing environmental data or configurations.ObjectNoneYes
plotThe plot configuration object, specifying how the plot should be rendered.ObjectNoneYes
setPlotFunction to update the plot configuration.FunctionNoneYes
elementsArray of elements or data points for the plot.ArrayNoneYes
subplotZoomsA state or context-managed object that retains zoom levels for subplots, enabling their use for further optimizations. If not provided, a local state within the component manages the zoom levels by default.ObjectlocalSubplotZoomsNo
setSubplotZoomsFunction to update the zoom levels of subplots.FunctionhandleSetSubplotZoomsNo
currentDragModeSpecifies the current drag mode for plot interaction.StringnullNo
selectionsA state object that holds the selection details which can be utilized for further actions or processing within the plot. This state can be managed by a parent component or through a context to enable interaction with selected elements.ObjectnullNo
setSelectionsFunction to update the selections on the plot.FunctionNoneNo
ignore_errorsDetermines whether to ignore errors during plotting.BooleantrueNo
onPlotUpdateCallback function for plot updates.FunctionnullNo
useDefaultModebarControls visibility of the default Plotly modebar.BooleantrueNo
additionalMenuItemsAdditional items for the custom menu.Array[]No
customSubplotEditorCustom component for editing subplots.React.ComponentnullNo
customColoraxisEditorCustom component for editing color axis.React.ComponentnullNo
useDefaultSchemaWhether to use the full Plotly schema.BooleantrueNo
childrenJSX or components to display when no plot is shown.React.NodeNoneNo
...restPropsAdditional props passed to the underlying Plot component.ObjectNoneNo

Additional Menu Items Example

The BasePlot component allows for the addition of custom menu items. Below is an example demonstrating how to define and use additionalMenuItems:

Defining Custom Menu Items

First, define your custom menu items. You can include icons and specify click event handlers:

const extraMenuItems = [
  {
    icon: <ion-icon name="heart-outline"></ion-icon>, // Icon for the menu item
    label: "Custom Item 1", // Label for the menu item
    onClick: () => console.log("Custom Item 1 clicked"), // Click handler for the menu item, add functionality that you want
  },
];

To use these extraMenuItems, simply pass them as a prop to the BasePlot component:

<BasePlot
  // ... other props
  additionalMenuItems={extraMenuItems}
/>

CustomMenu Component API Reference

The CustomMenu component provides a custom context menu interface, allowing for additional interaction with plot elements. It supports custom menu items and editors for subplot and color axis customization.

Props

KeyDescriptionTypeDefaultRequired
plotThe current plot configuration.ObjectNoneYes
setPlotFunction to update the plot configuration.FunctionNoneYes
elementsArray of elements or data points for the plot.ArrayNoneYes
elementThe element object representing the target plot area.ObjectNoneYes
contextThe context object containing environmental data or configurations.ObjectNoneYes
setShowLegendFunction to toggle the visibility of the legend.FunctionNoneYes
setSelectedElementFunction to set the currently selected plot element.FunctionNoneYes
additionalMenuItemsAdditional items to be added to the custom menu.Array[]No
customSubplotEditorA custom component for editing subplot details. If provided, this component replaces the default editor and must be capable of handling props for plot, setPlot, elements, context, and subplotName to interact with the plot's configuration.React.ComponentDefaultSubplotEditorNo
customColoraxisEditorA custom component for editing the color axis details. Similar to customSubplotEditor, this replaces the default color axis editor and must interface with the same props expected by the default editor.React.ComponentDefaultColoraxisEditorNo
useDefaultSchemaDetermines if the default or full Plotly schema should be used for the color axis editor.BooleantrueNo

Note: When using customSubplotEditor or customColoraxisEditor, ensure that the custom components you provide are designed to interact with the plot's data structure and update functions as the default editors do. If no custom editors are provided, CustomMenu utilizes its built-in editors that are already equipped to handle the necessary props and provide the appropriate UI for editing subplot and color axis details.

CustomMenuPopup Component API Reference

The CustomMenuPopup component displays a contextual popup with dynamic content. It is versatile and can adapt its position to prevent edge overflow, providing a smooth user experience.

Props

KeyDescriptionTypeDefaultRequired
contentThe content displayed inside the popup. This is typically a component or HTML elements.React.NodeNoneYes
clickPositionAn object detailing the popup's position, which should be a state updated upon a click. Refer to the CustomMenu source for implementation details. It needs to contain x and y coordinates, along with clientWidth and clientHeight for positioning calculations.ObjectNoneYes
setShowPopupA function that updates the visibility state of the popup, typically used to hide the popup when a click occurs outside of it.FunctionNoneYes

Example of state update

setPopupPosition({
  x: element.x,
  y: element.y,
  clientWidth: element.clientWidth,
  clientHeight: element.clientHeight,
});

Import

import { CustomMenuPopup } from "emerald-plotly-react";

elementPlotting API Reference

FunctionDescriptionParametersReturns
merge(a, b)Merges two objects, handling data types and combining arrays.a: Object, b: ObjectMerged Object
axis_tracename2layoutname(tracename)Converts a trace name to a layout name as per Plotly's convention.tracename: StringLayout Name String
split_axis_tracename(tracename)Splits a combined axis trace name into separate xaxis and yaxis.tracename: StringObject with xaxis and yaxis
underlaying_subplot(axis_tracename, plot)Determines the underlying subplot for a given axis trace name.axis_tracename: String, plot: ObjectSubplot Name String
instantiate_plot(plot, elements, context, ignore_errors)Instantiates a plot configuration with user-defined specs.plot, elements, context, ignore_errors: BooleanEnhanced Plot Object
subplot_specification(axis, plot_specification)Retrieves specification for a subplot based on axis names.axis: String, plot_specification: ObjectSubplot Specification Object
subplot_elements(axis, plot_specification, elements, context)Determines applicable plot elements for a given subplot.axis: String, plot_specification, elements, context: ObjectSubplot Elements Object
component_schema(name, elements, context)Generates a JSON schema for a specified component.name: String, elements, context: ObjectJSON Schema Object
subplot_schema(axis, plot_specification, elements, context)Creates a JSON schema for subplots with axis unit compatibility.axis: String, plot_specification, elements, context: ObjectJSON Schema Object
apply_selections(elements, context, eventData, action)Applies a selection action to components in a plot.elements, context: Object, eventData: Event, action: FunctionNone

Usage

These functions are used in building dynamic plotting applications with Plotly, allowing for customization and interaction based on user input or data changes.

License

MIT

1.0.14

14 days ago

1.0.13

14 days ago

0.0.1

24 days ago

1.0.12

2 months ago

1.0.11

3 months ago

1.0.10

3 months ago

1.0.9

3 months ago

1.0.8

3 months ago

1.0.7

3 months ago

1.0.2

3 months ago

1.0.1

3 months ago

1.0.6

3 months ago

1.0.5

3 months ago

1.0.4

3 months ago

1.0.3

3 months ago

1.0.0

3 months ago