D3v5 Sankey Diagram add Drag&Drop - d3.js
I have a Sankey Diagram where I show connections between source and target sites.
Now I want to add a drag&drop functionallity to this chart, but I have difficulties as I did use transform/translate until now. The current Sankey Version works with [[x0,y0],[x1,y1]] coords and x+width/y+height and not with translate anymore.
I found an example with drag & drop using translate/transform here: https://bl.ocks.org/d3noob/013054e8d7807dff76247b81b0e29030
This is my current example
const {
DOM
} = new observablehq.Library;
var units = "Links";
var margin = {
top: 10,
right: 10,
bottom: 10,
left: 10
},
width = 900 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;
// the function for moving the nodes
function dragmove(d) {
console.log(d3.event);
/*???
sankey.update(graph);
*/
}
const d3color = d3.scaleOrdinal(d3.schemeCategory10);
const color = function(name) {
return d3color(name.replace(/ .*/, ""));
}
var format = function(d) {
const f = d3.format(",.0f");
return f(d);
}
// append the svg canvas to the page
var svg = d3.select("#chart")
//.attr("viewBox", [0, 0, width, height]);;
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
// Set the sankey diagram properties
const sankey = d3.sankey()
.nodeAlign(d3.sankeyJustify)
.nodeWidth(15)
.nodePadding(10)
.extent([
[margin.top, margin.left],
[width - margin.right, height - margin.bottom]
]);
const data = {
links: [{
source: 1,
target: 0,
value: 1
},
{
source: 3,
target: 2,
value: 24
},
{
source: 5,
target: 4,
value: 1
},
{
source: 5,
target: 6,
value: 1
},
{
source: 7,
target: 6,
value: 2
},
{
source: 9,
target: 8,
value: 2
},
{
source: 11,
target: 10,
value: 32
},
{
source: 11,
target: 4,
value: 32
},
{
source: 11,
target: 12,
value: 32
},
{
source: 11,
target: 13,
value: 32
},
{
source: 11,
target: 14,
value: 30
},
{
source: 11,
target: 15,
value: 32
},
{
source: 11,
target: 16,
value: 32
},
{
source: 17,
target: 6,
value: 3
},
{
source: 18,
target: 4,
value: 1
},
{
source: 19,
target: 2,
value: 62
},
{
source: 20,
target: 6,
value: 7
},
{
source: 21,
target: 6,
value: 1
},
{
source: 23,
target: 22,
value: 7
},
{
source: 23,
target: 4,
value: 18
},
{
source: 23,
target: 6,
value: 15
},
{
source: 23,
target: 24,
value: 2
},
{
source: 25,
target: 6,
value: 1
},
{
source: 26,
target: 4,
value: 1
},
{
source: 26,
target: 6,
value: 1
},
{
source: 28,
target: 27,
value: 24
},
{
source: 28,
target: 29,
value: 1
},
{
source: 28,
target: 2,
value: 24
},
{
source: 28,
target: 30,
value: 24
},
{
source: 28,
target: 10,
value: 24
},
{
source: 28,
target: 12,
value: 10
},
{
source: 28,
target: 31,
value: 12
},
{
source: 28,
target: 32,
value: 9
},
{
source: 28,
target: 33,
value: 24
},
{
source: 28,
target: 14,
value: 22
},
{
source: 34,
target: 6,
value: 1
},
{
source: 35,
target: 24,
value: 1
},
{
source: 36,
target: 27,
value: 12
},
{
source: 36,
target: 37,
value: 12
},
{
source: 36,
target: 14,
value: 12
},
{
source: 38,
target: 4,
value: 3
},
{
source: 38,
target: 6,
value: 1
},
{
source: 38,
target: 39,
value: 1
},
{
source: 40,
target: 22,
value: 8
},
{
source: 40,
target: 4,
value: 6
},
{
source: 40,
target: 41,
value: 1
},
{
source: 40,
target: 42,
value: 6
},
{
source: 43,
target: 30,
value: 21
},
{
source: 44,
target: 6,
value: 1
},
{
source: 45,
target: 10,
value: 1
},
{
source: 45,
target: 4,
value: 5
},
{
source: 45,
target: 42,
value: 2
},
{
source: 45,
target: 6,
value: 2
},
{
source: 46,
target: 4,
value: 3
},
{
source: 46,
target: 6,
value: 1
},
{
source: 47,
target: 4,
value: 7
},
{
source: 47,
target: 41,
value: 9
},
{
source: 47,
target: 42,
value: 6
},
{
source: 47,
target: 6,
value: 6
},
{
source: 48,
target: 6,
value: 1
},
{
source: 49,
target: 4,
value: 3
},
{
source: 49,
target: 6,
value: 13
},
{
source: 49,
target: 24,
value: 1
},
{
source: 50,
target: 4,
value: 1
},
{
source: 51,
target: 4,
value: 1
},
{
source: 51,
target: 41,
value: 6
},
{
source: 51,
target: 42,
value: 3
},
{
source: 51,
target: 6,
value: 11
},
{
source: 52,
target: 4,
value: 3
},
{
source: 52,
target: 6,
value: 1
},
{
source: 53,
target: 4,
value: 1
},
{
source: 53,
target: 39,
value: 1
},
{
source: 54,
target: 22,
value: 2
},
{
source: 54,
target: 41,
value: 7
},
{
source: 56,
target: 55,
value: 3
},
{
source: 57,
target: 27,
value: 4
},
{
source: 57,
target: 30,
value: 4
},
{
source: 57,
target: 10,
value: 4
},
{
source: 57,
target: 4,
value: 4
},
{
source: 57,
target: 58,
value: 4
},
{
source: 59,
target: 4,
value: 3
},
{
source: 59,
target: 60,
value: 2
},
{
source: 59,
target: 41,
value: 5
},
{
source: 59,
target: 6,
value: 5
},
{
source: 59,
target: 24,
value: 1
},
{
source: 61,
target: 4,
value: 1
},
{
source: 61,
target: 6,
value: 1
},
{
source: 62,
target: 6,
value: 1
},
{
source: 63,
target: 41,
value: 1
},
{
source: 63,
target: 6,
value: 1
},
{
source: 64,
target: 4,
value: 1
},
{
source: 65,
target: 6,
value: 1
}
],
nodes: [{
value: 1,
name: 'site1'
},
{
value: 1,
name: 'site2'
},
{
value: 110,
name: 'site3'
},
{
value: 24,
name: 'site4'
},
{
value: 95,
name: 'site5'
},
{
value: 2,
name: 'site6'
},
{
value: 78,
name: 'site7'
},
{
value: 2,
name: 'site8'
},
{
value: 2,
name: 'site9'
},
{
value: 2,
name: 'site10'
},
{
value: 61,
name: 'site11'
},
{
value: 222,
name: 'site12'
},
{
value: 42,
name: 'site13'
},
{
value: 32,
name: 'site14'
},
{
value: 64,
name: 'site15'
},
{
value: 32,
name: 'site16'
},
{
value: 32,
name: 'site17'
},
{
value: 3,
name: 'site18'
},
{
value: 1,
name: 'site19'
},
{
value: 62,
name: 'site20'
},
{
value: 7,
name: 'site21'
},
{
value: 1,
name: 'site22'
},
{
value: 17,
name: 'site23'
},
{
value: 42,
name: 'site24'
},
{
value: 5,
name: 'site25'
},
{
value: 1,
name: 'site26'
},
{
value: 2,
name: 'site27'
},
{
value: 40,
name: 'site28'
},
{
value: 174,
name: 'site29'
},
{
value: 1,
name: 'site30'
},
{
value: 49,
name: 'site31'
},
{
value: 12,
name: 'site32'
},
{
value: 9,
name: 'site33'
},
{
value: 24,
name: 'site34'
},
{
value: 1,
name: 'site35'
},
{
value: 1,
name: 'site36'
},
{
value: 36,
name: 'site37'
},
{
value: 12,
name: 'site38'
},
{
value: 5,
name: 'site39'
},
{
value: 2,
name: 'site40'
},
{
value: 21,
name: 'site41'
},
{
value: 29,
name: 'site42'
},
{
value: 17,
name: 'site43'
},
{
value: 21,
name: 'site44'
},
{
value: 1,
name: 'site45'
},
{
value: 10,
name: 'site46'
},
{
value: 4,
name: 'site47'
},
{
value: 28,
name: 'site48'
},
{
value: 1,
name: 'site49'
},
{
value: 17,
name: 'site50'
},
{
value: 1,
name: 'site51'
},
{
value: 21,
name: 'site52'
},
{
value: 4,
name: 'site53'
},
{
value: 2,
name: 'site54'
},
{
value: 9,
name: 'site55'
},
{
value: 3,
name: 'site56'
},
{
value: 3,
name: 'site57'
},
{
value: 20,
name: 'site58'
},
{
value: 4,
name: 'site59'
},
{
value: 16,
name: 'site60'
},
{
value: 2,
name: 'site61'
},
{
value: 2,
name: 'site62'
},
{
value: 1,
name: 'site63'
},
{
value: 2,
name: 'site64'
},
{
value: 1,
name: 'site65'
},
{
value: 1,
name: 'site66'
}
]
};
const graph = sankey(data);
const node = svg.append("g")
.attr("stroke", "#000")
.selectAll("rect")
.data(graph.nodes)
.join("rect")
.attr("class", "node")
.attr("x", d => d.x0)
.attr("y", d => d.y0)
.attr("height", d => d.y1 - d.y0)
.attr("width", d => d.x1 - d.x0)
.attr("fill", d => d.color = color(d.name))
.style("stroke", d => d3.rgb(d.color).darker(2))
.call(d3.drag()
.subject(function(d) { return d; })
.on("start", function() { this.parentNode.appendChild(this); })
.on("drag", dragmove));
node.append("title")
.text(d => d.name + ': ' + format(d.value))
const link = svg.append("g")
.attr("fill", "none")
.selectAll("g")
.data(graph.links)
.join("g")
.style("mix-blend-mode", "multiply")
.attr("class", "link");
const gradient = link.append("linearGradient")
.attr("id", d => (d.uid = DOM.uid("link")).id)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", d => d.source.x1)
.attr("x2", d => d.target.x0);
gradient.append("stop")
.attr("offset", "0%")
.attr("stop-color", d => color(d.source.name));
gradient.append("stop")
.attr("offset", "100%")
.attr("stop-color", d => color(d.target.name));
link.append("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", d => d.uid)
.attr("stroke-width", d => Math.max(1, d.width));
link.append("title")
.text(d => d.source.name + ' → ' + d.target.name + ': ' + format(d.value));
svg.append("g")
.style("font", "10px sans-serif")
.selectAll("text")
.data(graph.nodes)
.join("text")
.attr("x", d => d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6)
.attr("y", d => (d.y1 + d.y0) / 2)
.attr("dy", "0.35em")
.attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
.text(d => d.name);
/*
// the function for moving the nodes
function dragmove(d) {
d3.select(this).attr("transform",
"translate(" + d.x + "," + (
d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))
) + ")");
sankey.relayout();
link.attr("d", d3.sankeyLinkHorizontal());
}*/
rect.node {
/*cursor: move; */
fill-opacity: .9;
shape-rendering: crispEdges;
}
.node title {
pointer-events: none;
text-shadow: 0 1px 0 #fff;
}
.link {
fill: none;
stroke: #000;
stroke-opacity: .2;
}
.link:hover {
stroke-opacity: .5;
}
<body>
<div>
<svg id='chart'></svg>
</div>
</body>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/d3-sankey#0.12.3/dist/d3-sankey.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/#observablehq/stdlib"></script>
How can drag & drop work here?
Related
Not getting products data from the Array of nested object using react.js. error- products.filter is no a function in categoryPreview component
const SHOP_DATA = [ { title: 'Hats', items: [ { id: 1, name: 'Brown Brim', imageUrl: 'https://i.ibb.co/ZYW3VTp/brown-brim.png', price: 25, }, { id: 2, name: 'Blue Beanie', imageUrl: 'https://i.ibb.co/ypkgK0X/blue-beanie.png', price: 18, }, { id: 3, name: 'Brown Cowboy', imageUrl: 'https://i.ibb.co/QdJwgmp/brown-cowboy.png', price: 35, }, { id: 4, name: 'Grey Brim', imageUrl: 'https://i.ibb.co/RjBLWxB/grey-brim.png', price: 25, }, { id: 5, name: 'Green Beanie', imageUrl: 'https://i.ibb.co/YTjW3vF/green-beanie.png', price: 18, }, { id: 6, name: 'Palm Tree Cap', imageUrl: 'https://i.ibb.co/rKBDvJX/palm-tree-cap.png', price: 14, }, { id: 7, name: 'Red Beanie', imageUrl: 'https://i.ibb.co/bLB646Z/red-beanie.png', price: 18, }, { id: 8, name: 'Wolf Cap', imageUrl: 'https://i.ibb.co/1f2nWMM/wolf-cap.png', price: 14, }, { id: 9, name: 'Blue Snapback', imageUrl: 'https://i.ibb.co/X2VJP2W/blue-snapback.png', price: 16, }, ], }, { title: 'Sneakers', items: [ { id: 10, name: 'Adidas NMD', imageUrl: 'https://i.ibb.co/0s3pdnc/adidas-nmd.png', price: 220, }, { id: 11, name: 'Adidas Yeezy', imageUrl: 'https://i.ibb.co/dJbG1cT/yeezy.png', price: 280, }, { id: 12, name: 'Black Converse', imageUrl: 'https://i.ibb.co/bPmVXyP/black-converse.png', price: 110, }, { id: 13, name: 'Nike White AirForce', imageUrl: 'https://i.ibb.co/1RcFPk0/white-nike-high-tops.png', price: 160, }, { id: 14, name: 'Nike Red High Tops', imageUrl: 'https://i.ibb.co/QcvzydB/nikes-red.png', price: 160, }, { id: 15, name: 'Nike Brown High Tops', imageUrl: 'https://i.ibb.co/fMTV342/nike-brown.png', price: 160, }, { id: 16, name: 'Air Jordan Limited', imageUrl: 'https://i.ibb.co/w4k6Ws9/nike-funky.png', price: 190, }, { id: 17, name: 'Timberlands', imageUrl: 'https://i.ibb.co/Mhh6wBg/timberlands.png', price: 200, }, ], }, ] export default SHOP_DATA; const UiUxCategories = () => { return ( <div> { Object.keys(SHOP_DATA).map((key) => { const products = SHOP_DATA[key]; return ( <CategoryPreview key={key} title={key} products={products} /> ) }) } </div> ) } error- products.filter is no a function in categoryPreview component const CategoryPreview = ({ title, products }) => { return ( <div > <h2>{title}</h2> <div> {products .filter((_, idx) => idx < 4) .map((product) => ( <ProductCard key={product.id} product={product} /> ))} </div> </div> ); }
create slider animation with multiple traces in plotly js
I want to make create a slider animation with multiple traces in plotly js like the image below but it shows only trace 1. when I click on the play button it only shows the first trace lines without any warning or error in the console. I used version plotly-2-12.1. enter image description here let myArr = new Array(35).fill(0).map((d, i) => i + 1); `Plotly.plot("graph", { trace1: { x: myArr, y: [0, -5, 2, 5, 15, 1, 2, 5, 15, , 20, 5, 17], type: "scatter", id: myArr, mode: "lines+markers", text: "Asas", name: "rex", marker: { color: "rgb(128, 0, 128)", size: 8, }, line: { simplify: false }, transforms: [ { type: "filter", operation: "<=", target: myArr, value: 0.0, }, ], }, trace2: { x: myArr, y: [0, 10, 20, 5, 17, 80, 3, , 20, 5, 17], text: "Asas", name: "rex", type: "scatter", id: myArr, mode: "lines+markers", text: "as", marker: { color: "rgb(0, 0, 128)", size: 8, }, line: { simplify: false }, transforms: [ { type: "filter", operation: "<=", target: myArr, value: 0.0, }, ], }, data: [trace1, trace2], layout: { xaxis: { autorange: false, range: [1, 35] }, yaxis: { autorange: false, range: [0, 100] }, updatemenus: [ { type: "buttons", xanchor: "left", yanchor: "top", direction: "right", x: 0, y: 0, pad: { t: 60 }, showactive: false, buttons: [ { label: "Play", method: "animate", args: [ null, { transition: { duration: 500 }, frame: { duration: 500, redraw: false }, mode: "immediate", fromcurrent: true, }, ], }, { label: "Pause", method: "animate", args: [ [null], { transition: { duration: 500 }, frame: { duration: 500, redraw: false }, mode: "immediate", }, ], }, ], }, ], sliders: [ { currentvalue: { prefix: "t = ", xanchor: "right", }, pad: { l: 130, t: 30 }, transition: { duration: 500, }, steps: myArr.map((t) => ({ label: t, method: "animate", args: [ [t], { transition: { duration: 500 }, frame: { duration: 500, redraw: false }, mode: "immediate", }, ], })), }, ], }, frames: myArr.map((t) => ({ name: t, data: [{ "transforms[0].value": t }], })), });` enter image description here
How to create multi color vertical bar chart in d3.js (version 4 and up) using specific json data?
i am working the typescript project i will expect the multicolored bar Chart the Json data is: const OutputData = [ { Date: '01/01/18', TotalPercentage: 40, Data: [{ Percentage: 40, color: 'red' }] }, { Date: '02/01/18', TotalPercentage: 60, Data: [{ Percentage: 20, color: 'green' }, { Percentage: 20, color: 'red' }, { Percentage: 20, color: 'Yellow' }] }, { Date: '03/01/18', TotalPercentage: 75, Data: [{ Percentage: 35, color: 'blue' }, { Percentage: 10, color: 'green' }, { Percentage: 30, color: 'red' }] }, { Date: '04/01/18', TotalPercentage: 80, Data: [{ Percentage: 30, color: 'green' }, { Percentage: 50, color: 'red' }] }, { Date: '05/01/18', TotalPercentage: 95, Data: [{ Percentage: 95, color: 'red' }] }, { Date: '06/01/18', TotalPercentage: 100, Data: [{ Percentage: 70, color: 'blue' }, { Percentage: 30, color: 'red' }] } ]; Expected Output is
this Code working good including angular project <!DOCTYPE html> <meta charset="utf-8"> <body> <div> <svg id="stacked"></svg> </div> </body> <script src="http://d3js.org/d3.v4.min.js" charset="utf-8"></script> <script> const ExpectedData = [ { Date: '01', Percentage: 5, Data: [{ Percentage: 2, color: 'lightgray' }, { Percentage: 3, color: 'silver' }] }, { Date: '02', Percentage: 6, Data: [{ Percentage: 3, color: 'silver' }, { Percentage: 3, color: 'lightgray' }] }, { Date: '03', Percentage: 8, Data: [{ Percentage: 4, color: 'lightgray' }, { Percentage: 4, color: 'silver' }] }, { Date: '04', Percentage: 10, Data: [{ Percentage: 5, color: 'silver' }, { Percentage: 5, color: 'lightgray' }] }, { Date: '05', Percentage: 12, Data: [{ Percentage: 6, color: 'lightgray' }, { Percentage: 6, color: 'silver' }] }, { Date: '06', Percentage: 14, Data: [{ Percentage: 7, color: 'silver' }, { Percentage: 7, color: 'lightgray' }] }, { Date: '07', Percentage: 16, Data: [{ Percentage: 8, color: 'lightgray' }, { Percentage: 8, color: 'silver' }] }, { Date: '08', Percentage: 18, Data: [{ Percentage: 9, color: 'silver' }, { Percentage: 9, color: 'lightgray' }] }, { Date: '09', Percentage: 20, Data: [{ Percentage: 10, color: 'lightgray' }, { Percentage: 10, color: 'silver' }] }, { Date: '10', Percentage: 22, Data: [{ Percentage: 11, color: 'silver' }, { Percentage: 11, color: 'lightgray' }] }, { Date: '11', Percentage: 24, Data: [{ Percentage: 12, color: 'lightgray' }, { Percentage: 12, color: 'silver' }] } ]; const height = 300; const width = 550; const x = d3.scaleBand() .domain(ExpectedData.map(function(d) { return d.Date; })) .rangeRound([0, width]) .padding(0.3) .align(0.3); const y = d3.scaleLinear() .domain([0, d3.max(ExpectedData, function(d) { return d.Percentage; })]).nice() .rangeRound([height, 0]); const svg = d3.select('#stacked').style('Width', '600px').style('height', '450px').style('overflow-x', 'auto'); const g = svg.append('g').attr('transform', 'translate(40, 40)'); g.selectAll('.group') .data(ExpectedData) .attr('class', 'group') .enter().append('g') .each(function(d, i) { d.Data.map( (obj, j, arr) => { d3.select(this) .append('rect') .attr('class', 'bar') .attr('data-index', j) .attr('x', function(e) { return x(d.Date); }) .attr('width', x.bandwidth()) .style('fill', function(e) { return obj.color; }) .attr('y', function(e) { let sum = 0; arr.map((obj_1, k) => { if (k < j) { sum = sum + obj_1.Percentage; } }); return y(obj.Percentage + sum); }) .attr('height', function(e) { return height - y(obj.Percentage); }); }); }); g.append('g') .attr('class', 'axis axis--x') .attr('transform', 'translate(0,' + height + ')') .call(d3.axisBottom(x)); g.append('g') .attr('class', 'axis axis--y') .call(d3.axisLeft(y)); g.selectAll('rect') .on('mouseover', function(d) { d3.select(this).style('fill', 'gray'); }) .on('mouseout', function(d, i, j) { const index = j[i].attributes['data-index'].nodeValue; d3.select(this).style('fill', d.Data[index].color); }); </script> </body> </html>
Custom label values for Y axis in amcharts
Can I set Custom label values for Y axis in amcharts js? example : convert 10,20,30,... y-axis value to 'very low','low','high'
There are two solutions here. The first solution uses a labelFunction in your valueAxis to specify what label you want given the value being drawn on the chart, i.e valueAxes: [ { minimum: 0, maximum: 50, strictMinMax: true, labelFunction: function(value, valueText, valueAxis) { switch (value) { case 10: valueText = "Very Low"; break; case 20: valueText = "Low"; break; case 30: valueText = "Average"; break; case 40: valueText = "Above Average"; break; case 50: valueText = "High"; break; default: valueText = ""; } return valueText; } } ], Demo: var chart = AmCharts.makeChart("chartdiv", { type: "serial", theme: "light", valueAxes: [ { minimum: 0, maximum: 50, strictMinMax: true, labelFunction: function(value, valueText, valueAxis) { switch (value) { case 10: valueText = "Very Low"; break; case 20: valueText = "Low"; break; case 30: valueText = "Average"; break; case 40: valueText = "Above Average"; break; case 50: valueText = "High"; break; default: valueText = ""; } return valueText; } } ], dataProvider: [ { category: "cat-1", value: 32 }, { category: "cat-2", value: 41 }, { category: "cat-3", value: 27 }, { category: "cat-4", value: 29 }, { category: "cat-5", value: 22 }, { category: "cat-6", value: 11 }, { category: "cat-7", value: 46 }, { category: "cat-8", value: 18 }, { category: "cat-9", value: 32 }, { category: "cat-10", value: 32 } ], graphs: [ { fillAlphas: 0.9, lineAlpha: 0.2, type: "column", valueField: "value" } ], categoryField: "category" }); #chartdiv { width: 100%; height: 300px; } <script src="//www.amcharts.com/lib/3/amcharts.js"></script> <script src="//www.amcharts.com/lib/3/serial.js"></script> <script src="//www.amcharts.com/lib/3/themes/light.js"></script> <div id="chartdiv"></div> Note that this solution is slightly brittle in that you're depending on the value axis to generate the correct scale (increments of 10, for instance) and there isn't a guaranteed way to control that. The better solution is to use guides instead to draw your axis labels, lines and ticks at the appropriate points on the axis, while disabling the ones generated by the axis to ensure that you get the correct lines: valueAxes: [ { minimum: 0, maximum: 50, strictMinMax: true, //disable the axis' labels, gridAlpha and tickLength so you can //draw them using guides labelsEnabled: false, gridAlpha: 0, tickLength: 0, guides: [{ value: 10, tickLength: 5, lineAlpha: .15, label: "Very Low" },{ value: 20, tickLength: 5, lineAlpha: .15, label: "Low" },{ value: 30, tickLength: 5, lineAlpha: .15, label: "Average" },{ value: 40, tickLength: 5, lineAlpha: .15, label: "Above Average" },{ value: 50, tickLength: 5, lineAlpha: .15, label: "High" }] } ] Demo: var chart = AmCharts.makeChart("chartdiv", { type: "serial", theme: "light", valueAxes: [ { minimum: 0, maximum: 50, strictMinMax: true, labelsEnabled: false, gridAlpha: 0, tickLength: 0, guides: [{ value: 10, tickLength: 5, lineAlpha: .15, label: "Very Low" },{ value: 20, tickLength: 5, lineAlpha: .15, label: "Low" },{ value: 30, tickLength: 5, lineAlpha: .15, label: "Average" },{ value: 40, tickLength: 5, lineAlpha: .15, label: "Above Average" },{ value: 50, tickLength: 5, lineAlpha: .15, label: "High" }] } ], dataProvider: [ { category: "cat-1", value: 32 }, { category: "cat-2", value: 41 }, { category: "cat-3", value: 27 }, { category: "cat-4", value: 29 }, { category: "cat-5", value: 22 }, { category: "cat-6", value: 11 }, { category: "cat-7", value: 46 }, { category: "cat-8", value: 18 }, { category: "cat-9", value: 32 }, { category: "cat-10", value: 32 } ], graphs: [ { fillAlphas: 0.9, lineAlpha: 0.2, type: "column", valueField: "value" } ], categoryField: "category" }); #chartdiv { width: 100%; height: 300px; } <script src="//www.amcharts.com/lib/3/amcharts.js"></script> <script src="//www.amcharts.com/lib/3/serial.js"></script> <script src="//www.amcharts.com/lib/3/themes/light.js"></script> <div id="chartdiv"></div>
In v4, you can use adapters to achieve this.
var chart = AmCharts.makeChart("chartdiv", { type: "serial", theme: "light", valueAxes: [ { minimum: 0, maximum: 100, strictMinMax: true, labelsEnabled: false, gridAlpha: 0, tickLength: 0, guides: [{ value: 10, tickLength: 2, lineAlpha: .15, label: "Very Low" },{ value: 20, tickLength: 2, lineAlpha: .15, label: "Low" },{ value: 30, tickLength: 2, lineAlpha: .15, label: "Average" },{ value: 40, tickLength:2, lineAlpha: .15, label: "Above Average" },{ value: 50, tickLength: 2, lineAlpha: .15, label: "High" }] } ], dataProvider: [ { category: "cat-1", value: 32 }, { category: "cat-2", value: 41 }, { category: "cat-3", value: 27 }, { category: "cat-4", value: 29 }, { category: "cat-5", value: 22 }, { category: "cat-6", value: 11 }, { category: "cat-7", value: 46 }, { category: "cat-8", value: 18 }, { category: "cat-9", value: 32 }, { category: "cat-10", value: 32 } ], graphs: [ { fillAlphas: 0.9, lineAlpha: 0.1, type: "column", valueField: "value" } ], categoryField: "category" }); #chartdiv { width: 100%; height: 300px; } <script src="//www.amcharts.com/lib/3/amcharts.js"></script> <script src="//www.amcharts.com/lib/3/serial.js"></script> <script src="//www.amcharts.com/lib/3/themes/light.js"></script> <div id="chartdiv"></div>
dc.js scatter plot filtering dimension from selection of control
I am attempting to filter a dimension on a scatter and the plots rendered via a control (select box) and I am having trouble getting this to work. Perhaps I am thinking because the dimension returns an array [key, value, value] and I am trying to filter using a text value from the control. <div id="chart-scatter"></div> <select id="selection"> <option value="BranchA">Branch A</option> <option value="BranchB">Branch B</option> <option value="BranchC">Branch C</option> <option value="BranchD">Branch D</option> </select> var transactions = [ { accountType: 9, amount: 284, serviceName: "BranchE" }, { accountType: 7, amount: 716, serviceName: "BranchE" }, { accountType: 5, amount: 899, serviceName: "BranchD" }, { accountType: 8, amount: 781, serviceName: "BranchD" }, { accountType: 5, amount: 295, serviceName: "BranchA" }, { accountType: 9, amount: 770, serviceName: "BranchB" }, { accountType: 9, amount: 195, serviceName: "BranchE" }, { accountType: 5, amount: 335, serviceName: "BranchF" }, { accountType: 10, amount: 74, serviceName: "BranchF" }, { accountType: 10, amount: 223, serviceName: "BranchC" }, { accountType: 5, amount: 290, serviceName: "BranchD" }, { accountType: 10, amount: 485, serviceName: "BranchA" }, { accountType: 7, amount: 364, serviceName: "BranchE" }, { accountType: 9, amount: 509, serviceName: "BranchB" }, { accountType: 8, amount: 74, serviceName: "BranchC" }, { accountType: 9, amount: 442, serviceName: "BranchE" } ]; filter = crossfilter(transactions); dim = filter.dimension(function(d) { return [d.accountType, d.amount, d.serviceName]; }); grp = dim.group(); scatterChart = dc.scatterPlot("#chart-scatter"); scatterChart .width(380) .height(200) .margins({ top: 10, right: 20, bottom: 30, left: 40 }) .dimension(dim) .group(grp) .x(d3.scale.linear().domain([4., 11.])) .y(d3.scale.linear().domain([0., 1000.])) .renderHorizontalGridLines(true) .renderVerticalGridLines(true) .symbolSize(30) .highlightedSize(8) .colorAccessor(function(d) { return d.key[2]; }) .colors(d3.scale.ordinal() .domain(['BranchA', 'BranchB', 'BranchC','BranchD','BranchE', 'BranchF']) .range(["#fa3701", "#339933", "#bbbbbb","#aaaaaa","#999999","#888888"]) ); d3.select("#selection").on('change', function(){ dim.filter($("#selection").val()) scatterChart.redraw(); dc.redrawAll(); }) dc.renderAll(); from examples I have found this seems like the approach in various places, however none are really for a scatter that I can find and I am wondering what the difference would be given the dim = array JSFiddle
A group does not observe filters on its own dimension (https://github.com/crossfilter/crossfilter/wiki/Crossfilter-Gotchas#a-group-does-not-observe-its-dimensions-filters). Because you are filtering on the same dimension the group is defined on, the filter has no affect on the group. Define a 2nd dimension for the service name and put a default filter in place: serviceDim = filter.dimension(function(d) { return "" + d.serviceName; }) serviceDim.filter('BranchA') Then update your change function as follows: d3.select("#selection").on('change', function(){ serviceDim.filter($("#selection").val()) //scatterChart.redraw(); dc.redrawAll(); }) Here is an updated JSFiddle: https://jsfiddle.net/esjewett/uhvh23b0/