Add Number Chart
By this I mean a div with a number. That number could be something the developer calculates from the query results of crossfilter (say mean, median, variance...etc).
Should this be done in dc.js (for example, creating the "calculate variance" function etc, and allow them to make their own value), if so consider leveraging Jason Davies' science.js.
I can definitely see value in this, although it is fairly simple to do this in d3, but then I guess the user will have to worry about redrawing and hooking up with dc triggers. I am thinking creating a really generic widget that can be anchored using a selector and support rendering a value using a given closure. The user is free to use any library to implement the closure.
On Sat, Jul 14, 2012 at 6:51 PM, Miles McCrocklin < [email protected]
wrote:
By this I mean a div with a number. That number could be something the developer calculates from the query results of crossfilter (say mean, median, variance...etc).
Should this be done in dc.js (for example, creating the "calculate variance" function etc, and allow them to make their own value), if so consider leveraging Jason Davies' science.js.
Reply to this email directly or view it on GitHub: https://github.com/NickQiZhu/dc.js/issues/6
I've got one that I wrote for myself. You may or may not care about the "humanize/dehumanize" functions. I find them to be very useful. Also, of note, I use a d3 tween on the number so as the value changes, you'll see it count up/down from its existing value to its new value. Helps user see what happened to the number (see bostock's comments on object constancy and change blindness). I haven't gotten around to turning into issuing a pull request because some package deep in NPM doesn't like VS2012+Win8+x64.
dc.numberDisplay = function (parent, chartGroup) {
var _chart = dc.baseChart({});
_chart.doRender = function () {
var newValue = _chart.group().top(1)[0].value;
_chart.selectAll(".number-display")
.transition()
.duration(750)
.tween("text", function (d, i, a) {
var i = d3.interpolate(dehumanize(this.textContent), newValue);
return function (t) {
this.textContent = humanize(Math.round(i(t)));
};
})
//.text(humanize());
return _chart;
};
_chart.doRedraw = function () {
return _chart.doRender();
};
return _chart.anchor(parent, chartGroup);
};
function humanize(value) {
if (value >= 1000000) {
var val = (value / 1000000);
if (val.toFixed(1).length > 3)
return val.toFixed(0) + "M";
else
return val.toFixed(1) + "M";
}
if (value >= 1000) {
var val = (value / 1000);
if (val.toFixed(1).length > 3)
return val.toFixed(0) + "K";
else
return val.toFixed(1) + "K";
}
return value + "";
}
function dehumanize(text) {
if (text === "" || text === undefined)
return 0;
var num = parseFloat(text);
if (text.indexOf("K") !== -1)
return num * 1000;
if (text.indexOf("M") !== -1)
return num * 1000000;
return num;
}
HTML:
<div id="total-sales-chart" class="widget">
<strong>Total Sales</strong>
<h2>$<span class="number-display"></span></h2>
</div>
Thanks for contribution :+1: almost forgot about this request =) will definitely check it out soon.
I just added commit with the patch proposed @vongillern. I like the approach, but we need to do a few things prior to merging with master, like add tests, make the humanize an option (and I think we can leverage some of the d3.format functions), add docs, etc ...
@vongillern take a look at http://nickqizhu.github.io/dc.js/examples/number.html for an example of the new chart
looks great!
On Mon, Sep 16, 2013 at 9:52 AM, Jacob Rideout [email protected]:
@vongillern https://github.com/vongillern take a look at http://nickqizhu.github.io/dc.js/examples/number.html for an example of the new chart
— Reply to this email directly or view it on GitHubhttps://github.com/NickQiZhu/dc.js/issues/6#issuecomment-24515728 .
It works well. I appreciate your efforts. Can I have a question about the overheard. The Number Graph is lighter than other graph such as Rowchart in terms of overhead?
@elsdy They serve different purposes, so it they shouldn't probably be compared directly. The number display is for a single number, while the row chart handles any number of dynamic values given the filter. I suspect that the number display takes bit less to render, but I don't really know since it doesn't seem like something that makes sense to compare. Can you give me a bit more detail toward what you are thinking?
I agree with your opinion that the number display occurs less overhead to render. I just want to confirm it through your opinion.
You stated that comparing Number display and Rowchart isn't fair because its purpose little bit different. !!! Why I was asking is that before the number display feature showed up I used row chart for showing numbers such as average value or variance or std deviation. !!! (And I think some people use row chart or bar chart representation instead of Number display before Number display function showed up.)
Now because of using Number display function, I just have finished modifying my codes with Number display which I consider as very simple method showing number. and then I was guessing that the loading time for drawing time must be reduced.
How much improvement for page loading time with Number display instead of rowchart which was used for showing just number can be measured by development mode in Safari or Chrome. But It doesn't need to be executed because We know that just showing number is a lighter job than rendering a row chart or a bar chart.