google chart animation doesn't work - animation

I am trying to make animations work in google charts. For some reason animation doesn't trigger.
Its pretty simple code. I would appreciate any help.
google.setOnLoadCallback(drawChart);
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
function drawChart() {
var options = {
width: 400,
height: 240,
animation:{
'duration': 5000,
'easing': 'out',
},
vAxis: {minValue:0, maxValue:100}
};
var chart = new google.visualization.ColumnChart(document.getElementById('piechart'));
chart.draw(data, options);
}
var ch=0;
function change(){
if(ch==0){
data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 1],
['Eat', 20],
['Commute', 20],
['Watch TV', 20],
['Sleep', 17]
]);
ch=1;
}
else if(ch==1){
data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 10],
['Eat', 2],
['Commute', 12],
['Watch TV', 24],
['Sleep', 47]
]);
ch=0;
}
drawChart();
}
full code is on below JSFIddle
https://jsfiddle.net/hjnufh0o/

You're recreating the chart object everytime you call drawChart() which has the effect of destroying the existing chart and building a new one - so there is nothing to animate between.
https://jsfiddle.net/2foeopyv/
I've pulled out the declarations for chart and options from the drawChart() section, so its now updating the chart rather than trying to recreate it.

Related

Area chart - start filling from non zero y value

I am trying to create an area chart that looks like this where I am using bars:
var trace1 = {
x: ['2013-10-04 9:00:00', '2013-10-04 9:30:00', '2013-10-04 10:00:00', '2013-10-04 11:00:00', '2013-10-04 11:30:00', '2013-10-04 12:30:00'],
y: [20, 20, 10, 10, 20, 20],
type: 'bar',
base: [5,5,5,5,5,5],
mode: 'none'
};
var data = [trace1];
myDiv = document.getElementById('myDiv');
Plotly.newPlot(myDiv, data);
Pen
As you can see y axis starts from non zero value. Is it even possible?
Thanks
You can add this code to start y-axis 0
var layout = {
yaxis: {
rangemode: 'tozero' //or below
//range: [0, 25]
}
};
Plotly.newPlot(myDiv, data, layout);

React Native animations - translateX and translateY while scaling

My Animated.View has the following style:
{
transform: [
{
scale: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [initialScale, 1]
})},
{
translateX: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [startX, endX]
})},
{
translateY: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [startY, endY]
})},
]
}
When initialScale is 1 and the animation starts, I see the expected behavior: Animated.View starts at (startX, startY) and linearly moves to (endX, endY). However, when initialScale is 0.5 for example, the starting point of the view is not (startX, startY), the movement is not linear (a bit spheric) and the end point is still as expected - (endX, endY).
How can I scale my View while keeping a linear movement and expected start position?
Like the user #ArneHugo pointed out in the comments, the non-linear movement can be solved by positioning the full-size container element and scaling another element within it.
The position of the element is not as expected, because the origin for the scale transform is the center point of the element. React Native doesn't (yet) support specifying the transform origin, but if the width and height of the scaled element are known in advance, it's easy to calculate the offset as follows:
const width = 100;
const height = 20;
const scale = {
transform: [
{
scale: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [initialScale, 1]
})
}
]
};
const position= {
transform: [
{
translateX: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [startX - (width / 2) - (width * initialScale / 2), endX]
})
},
{
translateY: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [startY - (height / 2) - (height * initialScale / 2), endY]
})
}
]
};
return (
<Animated.View style={position}>
<Animated.View style={[styles.thing, scale]} />
</Animated.View>
);
I've solved this problem by using scale factor for positioning:
imageScale = new Animated.Value(1);
imageX = new Animated.Value(0);
imageX = new Animated.Value(0);
...
<Animated.Image source={item} style={[styles.image, {
transform:[
{scale: this.imageScale},
{translateX: Animated.divide(this.imageX,this.imageScale)},
{translateY: Animated.divide(this.imageY,this.imageScale)},
]
}]}/>
If you use interpolation, this looks a bit complicated but the idea remains the same:
transform: [
{
scale: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [initialScale, 1]
})},
{
translateX: Animated.divide(
this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [startX, endX]
}),
this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [initialScale, 1]
})
)
}]

DimpleJS legend limited to part of the series

I'm using DimpleJS to render a BubblePlot. My data looks like this:
[
{type: "A", name:"First", x:1, y:200},
{type: "A", name:"Second", x:30, y:10},
{type: "B", name:"Third", x:50, y:120},
{type: "B", name:"Fifth", x:90, y:100}
]
The graph is created with:
var myChart = new dimple.chart(svg, chartData);
myChart.setBounds(50, 30, 370, 230);
var x = myChart.addMeasureAxis("x", "x");
var y = myChart.addMeasureAxis("y", "y");
var series = myChart.addSeries(["type", "name"], dimple.plot.bubble);
myChart.addLegend(10, 10, 360, 20, "right");
myChart.draw();
This nearly does what I want, with all the data available in the tooltips etc. But coloring is based on both typeand name.
Also unfortunately the legend also picks up all the values from the name field where I'd prefer to just see the type values within the legend.
I also tried to the use the addColorAxismethod like this:
var c = myChart.addColorAxis("type");
var series = myChart.addSeries("name", dimple.plot.bubble);
But that renders black bubbles, shows "NaN" as type in the tooltips and putting that into a legend also doesn't seem to be possible.
Any suggestions are welcome!
Turns out that the order of arguments in the series is important.
This solved my problem:
var myChart = new dimple.chart(svg, chartData);
myChart.setBounds(50, 30, 370, 230);
var x = myChart.addMeasureAxis("x", "x");
var y = myChart.addMeasureAxis("y", "y");
var series = myChart.addSeries(["name","type"], dimple.plot.bubble);
myChart.addLegend(10, 10, 360, 20, "right");
myChart.draw();

Problems with .x() in dc.js barCharts

I am doing my first interactive visualization using dc.js. I have come to a hard stop over a dc.barChart() attribute that I cannot seem with the answers I find here og on google. I will start the code from the crossfilter()function. I am able to create dc.rowCharts() so I highly doubt that it is a malformed .json problem
var ndx = crossfilter(salgsTransaksjonene);
//Define Dimensions
var kundeDim = ndx.dimension(function(d) { return d["CustomerName"]; });
//Calculate metrics
var OmsetningKunder = kundeDim.group().reduceSum(function(fact) { return fact.TotalAmount;});
//Charts
var toppChart = dc.barChart("#topp-20-bar-chart");
toppChart
.width(950)
.height(240)
.margins({top: 10, right: 50, bottom: 30, left: 50})
.dimension(kundeDim)
.group(topp20OmsetningKunder)
.transitionDuration(500)
.elasticY(true)
.yAxis().ticks(4)
When it comes to the .x() attribute I have tried the following
// .isOrdinal(true)
// .xUnitCount(20)
.xUnits(dc.units.ordinal.domain(salgsTransaksjonene.map(function (d) {return d.CustomerName; })))
.x(d3.scale.ordinal());
// .isOrdinal(true)
// .xUnitCount(20)
.xUnits(dc.units.ordinal)
.x(d3.scale.ordinal());
The errors I get is mostly the same; Uncaught TypeError: undefined is not a function
and various combinations of the above attributes, but to no avail. Any Ideas about what could be wrong is greatly appreciated!
Try to add a .domain with your ordinal values like this.
Also I've seen the order of the declarations to be of importance.
.x(d3.scale.ordinal().domain(['cat1','cat2','cat3']))
.xUnits(dc.units.ordinal)
here is a full working barchart for your inspiration.
chart11
.width(xxx)
.height(xxx)
.margins(xxx)
.dimension(xxx)
.group(xxx)
.x(d3.scale.ordinal().domain([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
.xUnits(dc.units.ordinal)
.centerBar(false)
.transitionDuration(transitionDuration)
.elasticY(true)
.gap(1)
.renderHorizontalGridLines(true)
.yAxis().ticks(2)
;

Google chart how to show display time statistic

Hi I want to show in google chart two column. First with time (eg. 06:02) second with value (eg. 3).
I try:
<script type="text/javascript">
google.load('visualization', '1', {packages: ['corechart']});
</script>
<script type="text/javascript">
function drawVisualization() {
var data = new google.visualization.DateFormat();
// Declare columns
data.addColumn('datetime', 'Time of Day');
data.addColumn('number', 'Some Measurement');
// Add data.
data.addRows([
[06:02,1],
[07:02,1],
[08:02,2],
[12:02,15],
[13:02,2],
[14:02,1],
[15:02,1],
[16:02,1],
[21:02,2],
]);
// Create and draw the visualization.
new google.visualization.LineChart(document.getElementById('visualization')).
draw(data, {curveType: "function",
width: 500, height: 400,
vAxis: {maxValue: 10}}
);
}
google.setOnLoadCallback(drawVisualization);
I try google doc but I must be doing something wrong...
It does'n work... Any idea...
Type and format of data is wrong, should be timeofday and [hour, min, sec, milli]. It seems that the last one, milliseconds, could be omitted. Additionally, DataTable has to be created:
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
function drawVisualization() {
var data = new google.visualization.DataTable();
// Declare columns
data.addColumn('timeofday', 'Time of Day');
data.addColumn('number', 'Some Measurement');
// Add data.
data.addRows([
[[6, 2, 0], 1],
[[7, 2, 0], 1],
[[8, 2, 0], 2],
[[12, 2, 0], 15],
[[13, 2, 0], 2],
[[14, 2, 0], 1],
[[15, 2, 0], 1],
[[16, 2, 0], 1],
[[21, 2, 0], 2]
]);
// Create and draw the visualization.
new google.visualization.LineChart(document.getElementById('visualization')).
draw(data, {
curveType: "function",
width: 500, height: 400,
vAxis: {maxValue: 10}
}
);
}
google.setOnLoadCallback(drawVisualization);
</script>
If your measurements are over several days then you will have to change the chart according to explanation in Google charts, timeofday starts at 8:00 and ends at 7:45

Resources