更新折线图(Updating line graph)
我正在寻找更新我的折线图的最佳方法。 传入的数据不是“新的”,它只是在许多不同的数据集之间切换。 我的问题是.append('path')根本不起作用,我不知道为什么。 它可能与我的分组有关:我喜欢在图表周围加上边距,因此轴和标签不会被剪裁。 为此,我的HTML看起来如下......
<svg w, h id='svg-container'> <g translate(w - margin, h - margin) id='chart-container'> <g id='x-axis'> <g id='y-axis'>
在我的代码中,我想选择
chart-container
并附加<path d='...' id='line'>
。 稍后,在更新图表时,我希望能够简单地select('chart-container').select('path')
并更新该选择。图表的初始设置:
var svgContainer = d3.select(element[0]).append('svg') .attr({ width: width + margin.left + margin.right, height: height + margin.top + margin.bottom }) .classed('svg-container', true); var chartContainer = svgContainer.append('g') .classed('chart-container', true) .attr('transform', "translate(" + margin.left + "," + margin.top + ")"); chartContainer.append('path') .data(activeData) .attr('d', lineFunc) .attr('stroke', 'blue') .attr('stroke-width', .attr('fill', 'none');
稍后更新图表:
svgContainer = d3.select('#line-container').select('.svg-container') .attr({ width: width + margin.left + margin.right, height: height + margin.top + margin.bottom }); chartContainer = svgContainer.select('.chart-container') .attr('transform', "translate(" + margin.left + "," + margin.top + ")"); chartContainer.select('path') .data(activeData) .attr('d', lineFunc) .attr('stroke', 'blue') .attr('stroke-width', .attr('fill', 'none');
不幸的是,这不起作用,我不知道为什么。 最初的
chartContainer.append('path')
似乎正在工作(有时),而chartContainer.select('path')
实际上并没有返回正确的项目(othertimes)。 可能d3要遍历我的轴组并返回其中一条路径? 我是以错误的方式来做这件事的吗?I'm looking for the best way to update my line graph. The incoming data is not 'new', it's just switching between many different data sets. My issue is that .append('path') simply isn't working, and I'm not sure why. It may have to do with my grouping: I like to put margins around my graphs so axis and labels aren't clipped. To do this, my HTML looks like the following...
<svg w, h id='svg-container'> <g translate(w - margin, h - margin) id='chart-container'> <g id='x-axis'> <g id='y-axis'>
In my code I would like to select
chart-container
and append a<path d='...' id='line'>
. Later, when updating the graph, I would hope to be able to simplyselect('chart-container').select('path')
and update that selection.Initial setup of the chart:
var svgContainer = d3.select(element[0]).append('svg') .attr({ width: width + margin.left + margin.right, height: height + margin.top + margin.bottom }) .classed('svg-container', true); var chartContainer = svgContainer.append('g') .classed('chart-container', true) .attr('transform', "translate(" + margin.left + "," + margin.top + ")"); chartContainer.append('path') .data(activeData) .attr('d', lineFunc) .attr('stroke', 'blue') .attr('stroke-width', .attr('fill', 'none');
Update the chart later on:
svgContainer = d3.select('#line-container').select('.svg-container') .attr({ width: width + margin.left + margin.right, height: height + margin.top + margin.bottom }); chartContainer = svgContainer.select('.chart-container') .attr('transform', "translate(" + margin.left + "," + margin.top + ")"); chartContainer.select('path') .data(activeData) .attr('d', lineFunc) .attr('stroke', 'blue') .attr('stroke-width', .attr('fill', 'none');
Unfortunately this doesn't work, and I'm not sure why. The initial
chartContainer.append('path')
seem to be working (sometimes), and thechartContainer.select('path')
doesn't actually return the correct item (othertimes). Might d3 be traversing my axis groups and returning one of their paths? Am I going about this the wrong way?
原文:https://stackoverflow.com/questions/18992415
最满意答案
即使按字典顺序,2位数的日期和工作号仍应正确排序。 但是,如果您涉及到2000之前的数据,那么您需要:
(包括样本表 - 只需忽略)
drop table if exists jobnumbers ; create table jobnumbers (id int primary key, number varchar(10)); insert jobnumbers values ( 1, 'J09-1893'), ( 2, 'J11-1323'), ( 3, 'J08-0011'), ( 4, 'J09-0234'), ( 5, 'J10-1232'), ( 6, 'J11-1111'), ( 9, 'J99-1111'); select * from jobnumbers order by case when number>'J5' then 1900 else 2000 end desc, number desc;
至于
indexing
(因为你标记了它),有两个选择:
- 你必须将它分成多个列才能索引年份,然后是工作号码
- 使用4位数年份。
The 2-digit dates and job numbers should still sort correctly, even lexicographically. But, if you had pre-2000 data involved, then you need this:
(sample table included - just ignore)
drop table if exists jobnumbers ; create table jobnumbers (id int primary key, number varchar(10)); insert jobnumbers values ( 1, 'J09-1893'), ( 2, 'J11-1323'), ( 3, 'J08-0011'), ( 4, 'J09-0234'), ( 5, 'J10-1232'), ( 6, 'J11-1111'), ( 9, 'J99-1111'); select * from jobnumbers order by case when number>'J5' then 1900 else 2000 end desc, number desc;
As for
indexing
(since you tagged it), two options:
- you would have to split that into multiple columns to be able to index on year, then job number
- Use 4-digit years.
相关问答
更多-
Oracle SQL中的自定义顺序(Custom Order in Oracle SQL)[2023-09-07]
不知道这是否符合简单: order by case when currency = 'USD' then 1 when currency = 'BHT' then 2 when currency = 'JPY' then 3 when currency = 'MYR' then 4 else 5 end 或者比较紧凑但是Oracle具体: order by decode(currency, 'USD', 1, 'B ... -
在CASE ,您可以将数值归于每个,并对这些值进行排序。 如果您需要查询大表,请考虑在Description上添加索引以提高排序性能。 ORDER BY CASE WHEN Description = 'Gold - owned' THEN 0 WHEN Description = 'Silver - identified / offered' THEN 1 WHEN Description = 'Bronze - no land' THEN 2 ELSE 99 /* ...
-
在sql查询中自定义顺序(Custom order by in sql query)[2021-12-17]
试试以下: ORDER BY CASE Category WHEN 'Pettycash' THEN '1' WHEN 'DailyExpense' THEN '2' WHEN 'Home Expense' THEN '3' WHEN 'Fair Expense' THEN '4' WHEN 'Cash Expense' THEN '5' ELSE '6' END 在此处找到文档。 Try following: ORDER BY CASE Category WH ... -
按规则创建自定义SQL顺序(Create Custom SQL Order By Rules)[2023-10-21]
即使按字典顺序,2位数的日期和工作号仍应正确排序。 但是,如果您涉及到2000之前的数据,那么您需要: (包括样本表 - 只需忽略) drop table if exists jobnumbers ; create table jobnumbers (id int primary key, number varchar(10)); insert jobnumbers values ( 1, 'J09-1893'), ( 2, 'J11-1323'), ( 3, 'J08-0011'), ( 4, ... -
我不认为SQL提供程序支持将lambda列添加到查询中。 但是,它确实支持coalesce操作符,因为你基本上是这样做的: SELECT * FROM Tasks ORDER BY ISNULL(DueDate, DATEADD(year, 10, GETDATE())) 尝试这个: from o in dc.Orders orderby o.DueDate ?? DateTime.Today.AddYears(10) 如果你需要假的,将来的日期也出于某种原因在你的结果集中,你可以在LINQtoSQL ...
-
自定义ORDER BY函数SQL(Custom ORDER BY Function SQL)[2023-04-16]
一个选项是CTE: with ordering as ( select v.* from (values(1, 'val1'), (2, 'val2'), (3, 'val3')) v(priority, val) ) select e.* from example e order by (select priority from ordering o where o.val = e.name); 我会阻止你进行显式join ,因为这可能会影响查询的语义。 在select ... -
SQL是通过Universe确定的,对象被拖入WebI文档的顺序确实影响了BO如何生成SQL。 听起来您正在寻找更改from子句的构建方式,这几乎总是需要对Universe进行更改,而不是将对象引入WebI查询构建器的顺序。 您可能能够更好地了解如何更改Universe以在Dave Rathbun的博客上生成正确/更好的SQL,尤其是他关于上下文的文章。 Business Objects has a super secret setting in the Universe Designer Properti ...
-
如果没有表格设计和字段,列,键,......,很难帮助你。 但我会体谅: 1 - 主键字段,以及如何关联表 2 - 如何使用“过滤器”添加左连接,或如何减少结果数量 3 - 评估使用子查询是否更好 另外:尝试使用TOP 100 <---进行查询以进行测试。 请记住:有时由于硬件限制(例如RAM)而优化查询是不可能的,在这种情况下,您必须分段显示数据。 It's dificult to help you without the tables design and fields|columns, keys, . ...
-
尝试这个: SELECT x FROM t1 ORDER BY CASE WHEN x = 1 THEN 100000001 WHEN x between 2 and 7 THEN 7 - x WHEN x between 8 and ( SELECT max(x) FROM t1 ) - 1 THEN x ELSE 100000000 END 1000000个常数必须比N大。 这是一个简单的演示 Try this: SELECT x F ...
-
一种解决方案是只使用子查询: SELECT t.* FROM ((SELECT 'First Query' as QRY, Field1, Field2 FROM Tabl1 WHERE Field1 <= 5 ) UNION ALL (SELECT 'Second Query', Field1, Field2 FROM Tabl1 WHERE Field1 > 5 ) ) t ORDER BY QRY, ...