简体   繁体   English

将其他数据点添加到图表

[英]Adding additional data points to a chart

I am creating a simple bar chart (using dimple.js ) showing participation in a chat room. 我正在创建一个简单的条形图(使用dimple.js )显示参与聊天室。 The Y axis are the different participants and the X axis is the amount of messages they have sent to the room. Y轴是不同的参与者,X轴是他们发送到房间的消息量。

The chart I am creating is very simple, and based on data from three columns (participantID, Participant_Name, Message_Count): 我创建的图表非常简单,并且基于三列(participantID,Participant_Name,Message_Count)的数据:

var svg = dimple.newSvg("#chrt_participants", 1200, 600);
var chrt_participants = new dimple.chart(svg, data.result);
chrt_participants.addCategoryAxis("y", "Participant_Name");
chrt_participants.addMeasureAxis("x", "Message_Count");
chrt_participants.addSeries(null, dimple.plot.bar);
chrt_participants.draw();

I would like to associate the participantID into the chart, so that when someone for instance clicks on a bar, that ID is sent via an ajax call somewhere else. 我想将participantID与图表相关联,这样当有人点击某个栏时,该ID就会通过其他地方的ajax调用发送。 The challenge I am having is that I seem to only be able to access the values I explicitly send to the chart when creating it (via the addCategoryAxis and addMeasureAxis functions) and I am not able to assign any more values to a bar. 我遇到的挑战是,我似乎只能访问我在创建它时显式发送给图表的值(通过addCategoryAxisaddMeasureAxis函数),而且我无法addMeasureAxis分配任何值。

So my question is, how do you assign more variables to an element of a chart, when those variables do not have any "visual purpose"? 所以我的问题是,当这些变量没有任何“视觉目的”时,如何为图表元素分配更多变量?

This difficulty here is that dimple works on an aggregated data set so your original data rows will not be directly accessible. 这里的困难在于,凹坑适用于聚合数据集,因此您的原始数据行将无法直接访问。 This can be worked around if you pre-aggregate your data to the level you require it and then tell dimple to disaggregate the series by your desired value (which will have no effect on the drawing), this will make the extra value accessible. 如果您将数据预先汇总到您需要的水平,然后告诉酒窝按照您想要的值分解系列(这对绘图没有影响),这可以解决,这将使额外的值可访问。 In this fiddle I update a label using the volume field which I associate with the series, even though it isn't drawn. 在这个小提琴中,我使用与系列相关联的音量字段更新标签,即使它没有被绘制。

chart = new dimple.chart(svg, data);
x = chart.addCategoryAxis("x", ["Fruit", "Year"]);
x.addGroupOrderRule([2012, 2013]);
chart.addMeasureAxis("y", "Value");
s = chart.addSeries(["Volume", "Year"], dimple.plot.bar);
s.addEventHandler("click", function (e) { 
d3.select("#infoLabel")
    .text("In " + e.seriesValue[1] + " we sold " + 
           e.seriesValue[0]  + " " + e.xValue + "s!");
});
chart.draw();

See the fully working example here: http://jsfiddle.net/uafGn/ 请参阅此处的完整工作示例: http//jsfiddle.net/uafGn/

If you're using jQuery, I'd recommend giving all of them the same class 'example_class' (or something with a better name) 如果你正在使用jQuery,我建议给它们所有的同一个类'example_class'(或者更好的名字)

Then give them dynamic id to each of those that you could parse out like '[user_id]:[x_axis]:[y_axis] 然后给每个你可以解析出来的动态id,比如'[user_id]:[x_axis]:[y_axis]

Then, add a jquery function like: 然后,添加一个jquery函数,如:

$('.example_class').on('click', function(){
    my_parse_function($(this).attr('id'));
}

Or, better yet, go beyond me and study the new HTML5 data attribute 或者,更好的是,超越我并研究新的HTML5数据属性

http://html5doctor.com/html5-custom-data-attributes/ http://html5doctor.com/html5-custom-data-attributes/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM