Welcome Guest Search | Active Topics |

Programmatically control the zoom level
cashlance
#1 Posted : Friday, August 23, 2013 9:21:05 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 8/23/2013(UTC)
Posts: 4

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Hi,

Is it possible to control the zoomed extent of the chart from javascript code?

My actual requirement is that I have two charts that I need to keep synchronised at the same zoom level. So if you zoom in on one chart then the other chart also zooms in and vice versa.

Alex.
Dragan
#2 Posted : Friday, August 23, 2013 9:38:03 AM(UTC)
Rank: Advanced Member

Groups: Administrators, DataVizJavaScript, jQueryChart, jQueryDV, MvcChart, Registered
Joined: 1/3/2011(UTC)
Posts: 483

Thanks: 0 times
Was thanked: 87 time(s) in 87 post(s)
Hi Alex,

You can use the following code:
Code:
    <script type="text/javascript">
        function round(d) {
            return Math.round(100 * d) / 100;
        }

        var ohlcData = [];
        var volumeData = [];

        var date = new Date(2010, 0, 1);

        var high = Math.random() * 40;
        var close = high - Math.random();
        var low = close - Math.random();
        var open = (high - low) * Math.random() + low;

        ohlcData.push([date, round(high), round(low), round(open), round(close)]);

        var volume = 100 + 15 * Math.random();
        volumeData.push([date, round(volume)]);

        for (var day = 2; day <= 60; day++) {

            date = new Date(2010, 0, day);

            high = open + Math.random();

            close = high - Math.random();
            low = close - Math.random();
            var oldOpen = open;
            open = (high - low) * Math.random() + low;

            if (low > oldOpen) {
                low = oldOpen;
            }

            ohlcData.push([date, round(high), round(low), round(open), round(close)]);

            volume = volume + 10 * Math.random() - 5;

            volumeData.push([date, round(volume)]);
        }

        $(document).ready(function () {
            $('#jqChart').jqChart({
                legend: { visible: false },
                border: { lineWidth: 0, padding: 0 },
                tooltips: {
                    type: 'shared',
                    disabled: true
                },
                animation: { duration: 1 },
                axes: [
                        {
                            location: 'bottom',
                            zoomEnabled: true
                        },
                        {
                            type: 'linear',
                            location: 'left',
                            width: 30
                        }
                ],
                series: [
                          {
                              title: 'Price Index',
                              type: 'candlestick',
                              data: ohlcData,
                              priceUpFillStyle: 'white',
                              priceDownFillStyle: 'black',
                              strokeStyle: 'black'
                          }
                ]
            });

            $('#jqChartVolume').jqChart({
                legend: { visible: false },
                border: { lineWidth: 0, padding: 0 },
                tooltips: {
                    type: 'shared',
                    disabled: true
                },
                animation: { duration: 1 },
                axes: [
                        {
                            type: 'dateTime',
                            location: 'bottom',
                            zoomEnabled: true
                        },
                        {
                            type: 'linear',
                            location: 'left',
                            width: 30
                        }
                ],
                series: [
                            {
                                type: 'column',
                                data: volumeData,
                                fillStyle: 'black'
                            }
                ]
            });

            var isZooming = false;

            $('#jqChart').bind('axisZoom', function (e, data) {

                if (isZooming) {
                    return;
                }

                isZooming = true;

                var chart = data.chart;
                var axis = data.axis;

                var min = axis.actualVisibleMinimum;
                var max = axis.actualVisibleMaximum;

                var animation = $('#jqChartVolume').jqChart('option', 'animation');
                animation.enabled = false;

                var axes = $('#jqChartVolume').jqChart('option', 'axes');
                axes[0].visibleMinimum = min;
                axes[0].visibleMaximum = max;

                $('#jqChartVolume').jqChart('update');

                isZooming = false;
            });

            $('#jqChartVolume').bind('axisZoom', function (e, data) {

                if (isZooming) {
                    return;
                }

                isZooming = true;

                var chart = data.chart;
                var axis = data.axis;

                var min = axis.actualVisibleMinimum;
                var max = axis.actualVisibleMaximum;

                var animation = $('#jqChart').jqChart('option', 'animation');
                animation.enabled = false;

                var axes = $('#jqChart').jqChart('option', 'axes');
                axes[0].visibleMinimum = min;
                axes[0].visibleMaximum = max;

                $('#jqChart').jqChart('update');

                isZooming = false;
            });

        });

    </script>

</head>
<body>

    <div>
        <div>
            <div id="jqChart" style="width: 600px; height: 250px;">
            </div>
        </div>
        <div>
            <div id="jqChartVolume" style="width: 600px; height: 130px;">
            </div>
        </div>
    </div>
</body>
Best Regards,
Dragan Matek
jqChart Inc.
cashlance
#3 Posted : Saturday, August 24, 2013 3:51:09 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 8/23/2013(UTC)
Posts: 4

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Thanks Dragan,

I've hooked that code up but things have gotten pretty sluggish, this is especially apparent on a mobile device.
I think I'll have to throttle the amount of events I send to the 2nd chart when zooming the first chart - unless you have a better idea?

Alex.
Dragan
#4 Posted : Monday, August 26, 2013 11:44:17 AM(UTC)
Rank: Advanced Member

Groups: Administrators, DataVizJavaScript, jQueryChart, jQueryDV, MvcChart, Registered
Joined: 1/3/2011(UTC)
Posts: 483

Thanks: 0 times
Was thanked: 87 time(s) in 87 post(s)
Yes, currently you need to reduce the updates to increase the performance.
Best Regards,
Dragan Matek
jqChart Inc.
Users browsing this topic
Guest (2)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

FlatEarth Theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.104 seconds.