Engineering

Adding chart editing to your Recharts app

How much of a chart editor should you build yourself? We made the same edit in a Recharts report and a Graphy one to see what each route asks of your team.

9 min read

Featured image

Two ways to edit the same monthly revenue chart. Graphy has the series menu open on the chart, and Recharts has a settings panel beside it.


Say your product renders its charts with Recharts, and your customers would like to edit them. At first that might be giving charts a new title, or a line instead of columns. Then it’s their own colours, a note on the month something changed and of course you now need a way to undo an edit.

Each request needs a control, code that applies it to the chart and somewhere to keep the result.

A title, a chart type and a colour fit comfortably in a settings form. A note about one month is different, because the obvious way to add it is to click that month. If editing is to happen on the chart itself, someone has to decide what a click selects, where its menu appears and how each change is undone.

So how much of that editor do you want to build yourself?

If the level of customisability you want to offer is small, a few well-defined choices might get you far enough to keep Recharts, and write the editing controls yourself. A dedicated editor earns a closer look when customers need to select parts of a chart and change them where they are. Graphy, which we make, supplies those point-and-edit controls, but only for charts it renders.

To illustrate how the two libraries compare, we built one experimental chart twice with Recharts and with Graphy, and made the same edit in each.

Experiment: one report, built twice

We kept the test small, with one chart and one edit, so the differences are easy to spot. Both versions use six months of illustrative revenue figures and open on a purple column chart called Monthly revenue.

The edit is to:


  • rename it Revenue trend

  • switch it to a line

  • give the line a different colour

  • save

Then close the report, reopen it and reload the page to check that everything came back

In the example below, try the edit in the Recharts tab, then make it again in the Graphy tab. Each tab saves its own copy in your browser, so one won’t overwrite the other.

Press Edit chart in either tab to start.

In the Recharts tab, a form opens beside the chart with a title field, a Column/Line dropdown and three colour swatches. The Graphy tab has no form, so you edit the chart itself. Click the title to rename it, the empty background for a menu with the chart type, and the line for a menu with its colour.

Made in that order, the edit survives in both tabs, and the reopened report shows Revenue trend as a line in the colour you chose, even after a reload. Choose the colour while the chart still shows columns, though, and the tabs disagree. The Recharts line keeps your colour, but the Graphy beta in this demo brings it back purple.

Building the controls in Recharts

Our Recharts chart already knew how to draw itself. Making it editable meant giving it a few settings to read and a form to change them.

The chart reads three settings every time it renders. The title appears as a heading above the plot, the type decides whether we render BarChart or LineChart, and the colour goes to the bars’ fill or the line’s stroke. Trimmed down, the component looks like this:

const Chart = settings.type === 'line' ? LineChart : BarChart;
const colour = colours[settings.colour];

return (
  <Chart data={revenue}>
    {/* grid, axes and tooltip don't depend on the settings */}
    {settings.type === 'line'
      ? <Line dataKey="revenue" stroke={colour} />
      : <Bar dataKey="revenue" fill={colour} />}
  </Chart>
);
const Chart = settings.type === 'line' ? LineChart : BarChart;
const colour = colours[settings.colour];

return (
  <Chart data={revenue}>
    {/* grid, axes and tooltip don't depend on the settings */}
    {settings.type === 'line'
      ? <Line dataKey="revenue" stroke={colour} />
      : <Bar dataKey="revenue" fill={colour} />}
  </Chart>
);
const Chart = settings.type === 'line' ? LineChart : BarChart;
const colour = colours[settings.colour];

return (
  <Chart data={revenue}>
    {/* grid, axes and tooltip don't depend on the settings */}
    {settings.type === 'line'
      ? <Line dataKey="revenue" stroke={colour} />
      : <Bar dataKey="revenue" fill={colour} />}
  </Chart>
);
const Chart = settings.type === 'line' ? LineChart : BarChart;
const colour = colours[settings.colour];

return (
  <Chart data={revenue}>
    {/* grid, axes and tooltip don't depend on the settings */}
    {settings.type === 'line'
      ? <Line dataKey="revenue" stroke={colour} />
      : <Bar dataKey="revenue" fill={colour} />}
  </Chart>
);
const Chart = settings.type === 'line' ? LineChart : BarChart;
const colour = colours[settings.colour];

return (
  <Chart data={revenue}>
    {/* grid, axes and tooltip don't depend on the settings */}
    {settings.type === 'line'
      ? <Line dataKey="revenue" stroke={colour} />
      : <Bar dataKey="revenue" fill={colour} />}
  </Chart>
);

When someone presses Save changes, the app stores the settings with the report:

{"title":"Revenue trend","colour":"Teal","type":"line"}
{"title":"Revenue trend","colour":"Teal","type":"line"}
{"title":"Revenue trend","colour":"Teal","type":"line"}
{"title":"Revenue trend","colour":"Teal","type":"line"}
{"title":"Revenue trend","colour":"Teal","type":"line"}

Reopening the report reads them back into the same component. The grid, the $k tick labels and the tooltip format stay in the component code, untouched, so nothing about how the chart looks had to be rebuilt. That’s the best argument for this route.

Because the type and the colour are separate settings, the order doesn’t matter. Pick Teal while the chart still shows columns, switch to a line, and the line is teal. Bar and Line read the same value, so we didn’t write anything extra for that.

Every choice you add follows the same pattern: a control, a setting, a mapping to Recharts props and a decision about what the setting means for each chart type. Recharts’ customisation guide shows how far props and custom rendering will take you. It gets harder when a choice applies to one part of the chart. To let someone recolour one series in a chart with several, you also have to let them pick it, show it’s selected and keep the control from covering it.

Making the same edit in Graphy

Graphy draws charts from its own specification, so our second version started by recreating the chart rather than adding to it.

We described the chart as a Graphy spec with the same data, axis range and colours, then rendered it with Graphy’s editable renderer. Every control in that tab came with it: the editable title, the chart menu that opens from the background and the colour menu on the columns or the line. Those menus offer more than our edit needed. The chart menu also has data labels and a palette, and the line’s menu has thickness, line style, fill under the line and points. We didn’t design or build any of it.

We left out a settings panel to keep the comparison about editing on the chart. If your product wants one, Graphy supplies a panel built from ready-made sections for the title, axes, chart type and more, sharing the menus’ undo history. If you’d rather use your own controls, the panel and menus take their switches, sliders and colour pickers from a registry you can fill with your components, and a button of your own can change the chart by sending Graphy a command.

Switch between the tabs and you’ll see the two charts aren’t identical. Our Graphy version puts the axis on the right, labels it “Revenue ($k)” instead of formatting each tick, and sits in a framed card. For a six-month demo we left those differences in. For a chart your product depends on, you’d reproduce and check the details customers rely on, such as tooltip content, number formats, label spacing and how the chart resizes in your layout. Only the charts you move need that work.

That purple line only appears when you combine edits in a particular order. In the Graphy beta we tested, colouring the columns first and then switching to a line lost the colour change. Switching to a line first and then colouring it kept both edits after saving, and the Recharts form kept both in either order. It’s easy to miss, because each control works on its own. Test combined edits in any editor you evaluate, ours included.

We used Recharts 3.11.0 and @graphysdk/react 1.9.0-beta.1795022135575, the versions in the demo above, so you can try both orders yourself.

Your app still handles saving

In both versions, our report decided which changes to keep and where they went.

With Recharts, our form supplied three settings. The app held them as a draft while the editor was open, wrote them to storage on Save changes and restored the saved version on Cancel. Recharts drew whatever settings it was given.

With Graphy, the editor hands the app the whole chart spec. Committed edits arrive through the onSpecChange callback, and our app keeps the latest one as the draft. An edit can still be in progress when someone presses Save, so our handler calls commands.commit() to finish it before storing the spec. To reopen the chart, the app passes back the saved spec and the report’s data, plus any theme or plugins the chart takes from outside the spec. Undo history starts fresh, because saving the spec doesn’t save the undo stack.

The difference is what gets saved. Our Recharts save is a small settings object we designed, and the component code describes everything else. Our Graphy save is the chart spec as plain JSON, with the data kept separately. Graphy’s guide to saving a chart covers the details.

Our demo keeps everything in the browser’s local storage and uses illustrative data. We checked saving, reopening and reloading in Chromium, most recently on 20 November, plus cancelling back to the saved title in Recharts. We didn’t test a database, sync across devices or two people editing at once. A real product also has to decide which report and which user a change belongs to, check the user is allowed to make it and choose where it’s stored. That stays with your app whichever library draws the chart.

Here’s how the work divides between the two routes:



Keep Recharts and add a form
Use Graphy for the edited chart
You build

The controls, and how each choice changes the chart

A Graphy version of the chart

You get

The exact choices you define, on a chart that already works

Editing on the chart itself, plus the option to add a ready-made panel or custom controls if you want them

You save

Your own settings object

The chart spec from onSpecChange

Still yours

Draft, Save, Cancel, storage and permissions

The same, plus passing the saved spec back with its data and any theme or plugins

To run both versions or compare them line by line, download the React source, including the report shell, the storage code and the browser checks we ran.

Download the React source (ZIP)

Is editing worth switching chart libraries for?

Honestly, our take is it's not worth switching from Recharts to Graphy if a few well-defined controls cover what customers ask for. A form can handle renaming a chart, switching between a couple of chart types and choosing from colours you provide; you decide how each choice behaves, including what happens when two of them combine, and you keep the charts and interactions you’ve already tuned. That’s a good reason to stick with Recharts, and it may be all the editing your product needs.

The case for an editor grows when editing moves onto the chart, or becomes a core workflow in your app. That means clicking a line to change its colour, hiding an axis, moving the legend, adding a note to a data point, and undoing any of it in the order it happened. Each of those needs selection that shows what someone clicked, a menu that appears beside it without covering it, and one undo history across every kind of edit. Designing that and keeping it working as your charts change is a much bigger job than a settings form. We wrote about five things a chart editor has to get right if you want to see what’s involved.

Graphy supplies that for the charts it renders. In edit mode, clicking a bar, a line, an axis label, the legend, a grid line or the chart background opens a menu for it, and changes made there can be undone and redone. Annotations have their own mode for adding and moving them, which our demo didn’t use.

If that’s where your customers are heading, our recommendation is to test it on one chart before committing to anything bigger. Choose one you're aleady familiar with, preferably one customers have asked to change, and rebuild it in Graphy. Make the edits they ask for, then make them again in a different order. Save, reopen and reload. Look hard at whatever you’d miss most if it broke, such as a tooltip format or the way the chart resizes in your layout. If the editing earns its place and the chart still looks right, you have your answer for that chart, and your other charts can stay on Recharts while you decide.

Start with Graphy’s editing overview and read the licence and evaluation terms before you build a trial. Our embeddable chart editor page sets out what Graphy supplies and what your app keeps.

Image

WRITTEN BY

Nick Smith

Graphy team

Nick has 13+ years experience of wrestling with the challenge of how to make data clear and easy to understand. He writes about product, marketing, and all things product marketing.

get started

get started

Install and test it on your own machine.

Install the SDK and follow the docs to render your first chart. Get in touch when you're ready to use Graphy in production.

Install the SDK and follow the docs to render your first chart. Get in touch when you’re ready to use Graphy in production.

01 install

Add Graphy to your app

npm install @graphysdk/react

02 render

Render your first chart

<GraphProvider input={spec} data={data}>
  <GraphRenderer />
</GraphProvider>

03 go live

Use Graphy in production.

Tell us what you are building. We will help with the right licence, integration path and support.