Manipulate Chart Data with Mouse
A line chart you can edit by hand: press anywhere on the plot and drag up or down, and the nearest point follows. Handy for what-if plans and rough forecasts.
About this chart
When people edit the data
Dragging points turns a chart into an input: people shape a forecast, a budget or a target curve by eye and see the result as they go. The new values are in the chart’s data, ready for your code to save or send on.
Good for
- What-if planning and rough forecasts
- Teaching how one change moves a trend
- Collecting estimates in a survey
Think twice when
- Exact numbers: offer a table or input boxes too
- Hundreds of points: each one is too close to the next to pick
- Data that must not change, like reported results
For developers
Get the code.
Built with amCharts 5, a JavaScript library. The JavaScript includes your theme and renderer (canvas or SVG); the JSON also follows the changes you make with the controls.
For projects that use npm:
How it reads:
Using Claude or ChatGPT? Install the amCharts plugin first (Claude, ChatGPT), so it works from our docs and examples.
npm install @amcharts/amcharts5. See Getting started.
The same chart as a JSON config. Chart Maker reads and writes this format, the library's JSON parser turns it into a chart, and the <am5-chart> element puts it on a page with no code.How it reads:
"type" is the class to create and "settings" what you would pass to its new() in code; "properties" fill in its parts, lists such as series and axes or templates such as columns.template; "refs" holds objects used in more than one place ("#series-0" stands for the one named series-0); a string starting with "@" takes a value from another object, such as "@series.get('fill')".
A complete page. Save it as an .html file and open it in a browser.
Paste this into an AI coding assistant to build the chart from scratch. More in amCharts & AI.Using Claude or ChatGPT? Install the amCharts plugin first (Claude, ChatGPT), so it works from our docs and examples.
// Create root element
// https://docs.amcharts.com/v5/getting-started/#root-element
var root = am5.Root.new("chartdiv");
const myTheme = am5.Theme.new(root); // a theme of our own, for the rule below
myTheme.rule("AxisLabel").setAll({
fontSize:"0.9em" // axis labels at 90% of the normal text size
});
// Set themes
// https://docs.amcharts.com/v5/concepts/themes/
root.setThemes([
am5themes_Animated.new(root),
myTheme,
am5themes_ResponsiveMax.new(root)
]);
// Create chart
// https://docs.amcharts.com/v5/charts/xy-chart/
var chart = root.container.children.push(am5xy.XYChart.new(root, {
wheelX: "panX", // a horizontal wheel or trackpad swipe pans...
wheelY: "zoomX", // ...and the vertical wheel zooms in on the dates
pinchZoomX: true, // pinch with two fingers to zoom on a touch screen
paddingLeft: 0 // the value labels sit at the chart's left edge
}));
// Add cursor
// https://docs.amcharts.com/v5/charts/xy-chart/cursor/
var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {
behavior: "none" // a drag on the plot neither zooms nor selects, so it can move points
}));
cursor.lineY.set("visible", false); // only the vertical cursor line, no horizontal one
// Generate random data
var date = new Date();
date.setHours(0, 0, 0, 0);
var value = 100;
// one day's point: the value moves randomly by up to 5 from the day before
function generateData() {
value = Math.round((Math.random() * 10 - 5) + value);
am5.time.add(date, "day", 1);
return {
date: date.getTime(),
value: value
};
}
// an array of count daily points
function generateDatas(count) {
var data = [];
for (var i = 0; i < count; ++i) {
data.push(generateData());
}
return data;
}
// Create axes
// https://docs.amcharts.com/v5/charts/xy-chart/axes/
var xAxis = chart.xAxes.push(am5xy.DateAxis.new(root, {
maxDeviation: 0.2, // pans up to 20% past the first and last date
baseInterval: { // one data point per day
timeUnit: "day",
count: 1
},
renderer: am5xy.AxisRendererX.new(root, {
minorGridEnabled: true // fainter grid lines between the labeled ones
}),
tooltip: am5.Tooltip.new(root, {}) // a date label follows the cursor along the axis
}));
var yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
renderer: am5xy.AxisRendererY.new(root, {
// drag along the value axis labels to zoom it in and out
pan: "zoom"
})
}));
// Add series
// https://docs.amcharts.com/v5/charts/xy-chart/series/
var series = chart.series.push(am5xy.LineSeries.new(root, {
name: "Series",
xAxis: xAxis,
yAxis: yAxis,
valueYField: "value",
valueXField: "date",
tooltip: am5.Tooltip.new(root, {
labelText: "{valueY.formatNumber('#.#')}" // the value with up to one decimal
})
}));
// a hollow dot on each point, with an up-down pointer to hint it can be dragged
series.bullets.push(function (root, series) {
var graphics = am5.Circle.new(root, {
radius: 4, // 4px radius
interactive: true, // reacts to the pointer, so the pointer style shows
cursorOverStyle: "ns-resize", // an up-down arrow pointer over the dot
stroke: series.get("stroke"), // outlined in the line's color
// filled with the chart's background color, so the points look hollow in light and dark mode
fill: root.interfaceColors.get("background"),
// keyboard and screen readers (see Accessibility below): one Tab stop for all the points (a focusable group),
// the arrow keys step through them
focusable: true,
focusableGroup: "points",
ariaLabel: "{valueX.formatDate('MMMM d, yyyy')}: {valueY.formatNumber('#.#')}"
});
return am5.Bullet.new(root, {
sprite: graphics
});
});
// Add scrollbar
// https://docs.amcharts.com/v5/charts/xy-chart/scrollbars/
chart.set("scrollbarX", am5.Scrollbar.new(root, {
orientation: "horizontal" // a bar above the plot to zoom and scroll through the dates
}));
// manipulating with mouse code
var isDown = false; // true while the mouse button is held down on the plot
// register down
chart.plotContainer.events.on("pointerdown", function() {
isDown = true;
})
// register up
chart.plotContainer.events.on("globalpointerup", function() {
isDown = false;
})
// while the button is held, the point under the cursor follows the pointer up and down
chart.plotContainer.events.on("globalpointermove", function(e) {
// if pointer is down
if (isDown) {
// get tooltip data item
var tooltipDataItem = series.get("tooltipDataItem");
if (tooltipDataItem) {
if (e.originalEvent) {
// the pointer's height in the plot area, turned into a value on the Y axis
var position = yAxis.coordinateToPosition(chart.plotContainer.toLocal(e.point).y);
// rounded to one decimal
var value = Math.round(yAxis.positionToValue(position) * 10) / 10;
// need to set both working and original value
tooltipDataItem.set("valueY", value);
tooltipDataItem.set("valueYWorking", value);
}
}
}
})
// Set data
var data = generateDatas(40);
series.data.setAll(data);
// Accessibility: keyboard and screen reader support
// https://docs.amcharts.com/v5/concepts/accessibility/
chart.setAll({
focusable: true, // Tab reaches the chart, and a screen reader names it
role: "figure",
ariaLabel: "Line chart of a daily value over 40 days, whose points can be dragged up or down with a mouse or by touch. Tab to the points, then move between them with the arrow keys"
});
// the points: set up for the keyboard where they are made, in the bullet function above
// Make stuff animate on load
// https://docs.amcharts.com/v5/concepts/animations/
series.appear(1000);
chart.appear(1000, 100);
{
"type": "XYChart",
"settings": {
"wheelX": "panX",
"wheelY": "zoomX",
"pinchZoomX": true,
"paddingLeft": 0,
"cursor": {
"type": "XYCursor",
"settings": {
"behavior": "none"
},
"properties": {
"lineY": {
"settings": {
"visible": false
}
}
}
},
"scrollbarX": {
"type": "Scrollbar",
"settings": {
"orientation": "horizontal"
}
},
"focusable": true,
"role": "figure",
"ariaLabel": "Line chart of a daily value over 40 days, whose points can be dragged up or down with a mouse or by touch. Tab to the points, then move between them with the arrow keys"
},
"properties": {
"series": [
"#series-0"
],
"xAxes": [
"#axis-0"
],
"yAxes": [
"#axis-1"
]
},
"refs": [
{
"data-0": [
{
"date": 1795298400000,
"value": 102
},
{
"date": 1795384800000,
"value": 106
},
{
"date": 1795471200000,
"value": 110
},
{
"date": 1795557600000,
"value": 113
},
{
"date": 1795644000000,
"value": 111
},
{
"date": 1795730400000,
"value": 111
},
{
"date": 1795816800000,
"value": 107
},
{
"date": 1795903200000,
"value": 103
},
{
"date": 1795989600000,
"value": 102
},
{
"date": 1796076000000,
"value": 104
},
{
"date": 1796162400000,
"value": 107
},
{
"date": 1796248800000,
"value": 104
},
{
"date": 1796335200000,
"value": 105
},
{
"date": 1796421600000,
"value": 110
},
{
"date": 1796508000000,
"value": 105
},
{
"date": 1796594400000,
"value": 105
},
{
"date": 1796680800000,
"value": 107
},
{
"date": 1796767200000,
"value": 107
},
{
"date": 1796853600000,
"value": 106
},
{
"date": 1796940000000,
"value": 101
},
{
"date": 1797026400000,
"value": 96
},
{
"date": 1797112800000,
"value": 97
},
{
"date": 1797199200000,
"value": 95
},
{
"date": 1797285600000,
"value": 100
},
{
"date": 1797372000000,
"value": 100
},
{
"date": 1797458400000,
"value": 97
},
{
"date": 1797544800000,
"value": 101
},
{
"date": 1797631200000,
"value": 101
},
{
"date": 1797717600000,
"value": 99
},
{
"date": 1797804000000,
"value": 100
},
{
"date": 1797890400000,
"value": 101
},
{
"date": 1797976800000,
"value": 101
},
{
"date": 1798063200000,
"value": 99
},
{
"date": 1798149600000,
"value": 98
},
{
"date": 1798236000000,
"value": 97
},
{
"date": 1798322400000,
"value": 100
},
{
"date": 1798408800000,
"value": 95
},
{
"date": 1798495200000,
"value": 96
},
{
"date": 1798581600000,
"value": 91
},
{
"date": 1798668000000,
"value": 94
}
]
},
{
"axis-0": {
"type": "DateAxis",
"settings": {
"maxDeviation": 0.2,
"baseInterval": {
"timeUnit": "day",
"count": 1
},
"renderer": {
"type": "AxisRendererX",
"settings": {
"minorGridEnabled": true
}
},
"tooltip": {
"type": "Tooltip"
}
}
}
},
{
"axis-1": {
"type": "ValueAxis",
"settings": {
"renderer": {
"type": "AxisRendererY",
"settings": {
"pan": "zoom"
}
}
}
}
},
{
"series-0": {
"type": "LineSeries",
"settings": {
"name": "Series",
"xAxis": "#axis-0",
"yAxis": "#axis-1",
"valueYField": "value",
"valueXField": "date",
"tooltip": {
"type": "Tooltip",
"settings": {
"labelText": "{valueY.formatNumber('#.#')}"
}
}
},
"properties": {
"data": "#data-0",
"bullets": [
{
"type": "Bullet",
"settings": {
"sprite": {
"type": "Circle",
"settings": {
"radius": 4,
"interactive": true,
"cursorOverStyle": "ns-resize",
"stroke": "@series.get('stroke')",
"fill": "@root.interfaceColors.get('background')",
"focusable": true,
"focusableGroup": "points",
"ariaLabel": "{valueX.formatDate('MMMM d, yyyy')}: {valueY.formatNumber('#.#')}"
}
}
}
}
]
}
}
}
]
}



