这个问题的主题可能没有给出真实的场景,请阅读下面的所有内容,谢谢。
我正在开发基于以下数据的散点图(JSON - 在文件 simple.json 中):
{
"docs":
[
{"timestamp":"01","id":"100","quantity":"5","pay":"50","bp":"25","city":"Multan"},
{"timestamp":"02","id":"200","quantity":"10","pay":"100","bp":"50","city":"Lahore"},
{"timestamp":"03","id":"300","quantity":"3","pay":"30","bp":"15","city":"Multan"},
{"timestamp":"04","id":"400","quantity":"5","pay":"50","bp":"25","city":"Multan"},
{"timestamp":"05","id":"500","quantity":"6","pay":"60","bp":"30","city":"Lahore"},
{"timestamp":"06","id":"600","quantity":"15","pay":"150","bp":"75","city":"Islamabad"},
{"timestamp":"07","id":"700","quantity":"14","pay":"140","bp":"70","city":"Islamabad"},
{"timestamp":"08","id":"800","quantity":"18","pay":"180","bp":"90","city":"Islamabad"},
{"timestamp":"09","id":"900","quantity":"7","pay":"70","bp":"35","city":"Lahore"},
{"timestamp":"10","id":"1000","quantity":"20","pay":"200","bp":"100","city":"Islamabad"}
]
}
我正在尝试开发一个可重复使用的图表,我可以在其中向用户展示可用的数据列(来自上述数据)。因此,用户可以为 X 轴选择某一列(例如“id”),为 Y 轴选择另一列(例如“数量”)(直到这里一切都完美并符合预期)。稍后用户可以选择另一列并单击按钮在图表上绘制该列(以及先前添加的列)。
问题来了:
当我继续为 Y 轴处理另一列(比如“支付”)时,同时保持先前在图表上,新的被正确绘制(我也在根据新数据重新调整轴)。但旧的不重新排列。这是实际的问题。我正在考虑跟踪添加的每一列(通过将列引用存储在单独的数组中),所以每次有新列时,我都必须重新绘制旧列(我应该吗?)。但这在 D3 的功能或性能方面看起来并不可行。
为此,我还对绘制的每个圆圈应用了一个匿名类“更新”,这样我就可以选择所有“更新”圆圈,但这里出现了另一个问题,我怎么知道这些圆圈的新位置?我是否需要再次遍历该特定系列的数据?并且必须再画一遍?对于每个新系列,跟踪旧系列并重新绘制它们,将依次增加处理开销。是否有任何方便的解决方案或内置(d3's)机制来根据新比例重新调整以前的绘图?
请提出一些建议。我确信我缺少一些关键点。