Data Compression

Overview

AnyChart Stock component is an SWF file, which is loaded by the browser just as any HTML page with a flash object embedded in it. Since the data to be shown is usually located on the sever, any time you need to show a new data set, you should load it as well.

In the cases when you have really large data sets, while the bandwidth is limited, you can use data compression to save the bandwidth and improve user experience.

AnyChart Stock can accept zlib compressed CSV data sets. A data set can be either a compressed file or a script that outputs a compressed stream.

You can learn more about Zlib at: http://en.wikipedia.org/wiki/Zlib

to top

Sample

In this section we are going to demonstrate how to compress a CSV file named ixic_daily.csv on the server side using PHP and how to use it in AnyChart Stock.

Here is a simple PHP script that compresses the file and then saves it under a different name:

As the result, we get ixic_daily.csv.dat (138 KB) instead of ixic_dialy.csv (438 KB). Quite significant difference!

To use the compressed data in the component, specify it in the data set definition. For that purpose, set the compressed attribute in the <data_set> node to true.

Here is a sample XML for a data set that uses compressed data:

XML/JSON Syntax
Plain code
01 <?xml version="1.0" encoding="UTF-8"?>
02 <stock xmlns="http://anychart.com/products/stock/schemas/1.9.0/schema.xsd">
03   <data>
04     <data_sets>
05       <data_set id="dataSet1" source_mode="ExternalData" source_url="./ixic_daily.csv.dat" compressed="true">
06         <csv_settings ignore_first_row="true" rows_separator="\n" columns_separator="," />
07         <locale>
08           <date_time>
09             <format><![CDATA[%yyyy%MM%dd]]></format>
10           </date_time>
11         </locale>
12       </data_set>
13     </data_sets>
14     <data_providers>
15       <general_data_providers>
16         <data_provider data_set="dataSet1" id="dp1">
17           <fields>
18             <field type="Close" column="4" approximation_type="Close" />
19           </fields>
20         </data_provider>
21       </general_data_providers>
22       <scroller_data_providers>
23         <data_provider data_set="dataSet1" column="4" />
24       </scroller_data_providers>
25     </data_providers>
26   </data>
27 </stock>
01{
02  data: {
03    dataSets: [
04      {
05        id: "dataSet1",
06        sourceMode: "ExternalData",
07        sourceUrl: "./ixic_daily.csv.dat",
08        compressed: true,
09        csvSettings: {
10          ignoreFirstRow: true,
11          rowsSeparator: "\n",
12          columnsSeparator: ","
13        },
14        locale: {
15          dateTime: {
16            format: "%yyyy%MM%dd"
17          }
18        }
19      }
20    ],
21    dataProviders: {
22      generalDataProviders: [
23        {
24          dataSet: "dataSet1",
25          id: "dp1",
26          fields: [
27            {
28              type: "Close",
29              column: 4,
30              approximationType: "Close"
31            }
32          ]
33        }
34      ],
35      scrollerDataProviders: [
36        {
37          dataSet: "dataSet1",
38          column: 4
39        }
40      ]
41    }
42  }
43}

The live sample below uses both compressed and uncompressed data; the data and the chart are identical, but if you use the compressed data only, you can both save the bandwidth and speed the things up:

Live Sample:  Using Compressed Data

 

Note: The data set that uses compression should have all the same settings that the data set without compression has: CSV settings, locale, and so on.

Note: You cannot use a compressed data set with the InternalData mode (<data_set source_mode="InternalData">), as the XML cannot contain raw binary data.

Note: You can use compressed files, as shown in the sample above, and scripts that output a zlib-compressed stream. In this case, you simply specify the script's address instead of file's URL; for example, <data_set source_url="http://localhost/getdata.php">.

The preparation and the way you handle compressed data heavily depends on your application; please consider this sample and feature as an opportunity rather than the final solution, for the final solution is highly individual - perhaps you could use data streaming instead of using compression.

to top