his entry is part 3 of 4 in the series Dive Into Dojo Notice: There is a newer version of this post available
One of the most powerful pieces of Dojo is also one of the most underutilized: Charting. The Dojo Charting library lives within the DojoX (extensions) branch of Dojo, and features numerous chart types, options, and a variety of themes. This post introduce you to the charting library and show you how you can take a boring data collection and make it a beautiful visual chart in any modern web browser.
Requiring Charting ClassesA common misconception with many Dojo libraries is that they require a large number of dependencies — this is not the case. Dependencies change depending on the chart you’d like to make, but a simple chart can be created with only the following Dojo classes:
Two Ways to PlayMuch like most of the widgets within the Dijit and DojoX libraries, the charting library allows you to create charts programmatically and declaratively. Let’s take a look at an example of each approach. Sample DataThe following JSON is used for our simple Pie chart:
Declarative Chart CreationThe declarative method of creating charts is done with HTML markup. All chart data such as type, axis, plot, and other chart information is created within the chart container. Here’s a simple pie chart:
Programmatic Chart CreationProgrammatic chart creation is also quite easy. The JavaScript code below reproduces the chart above:
Take a moment to compare the two methods and you will quickly notice how similar the structures are. It’s the developer’s choice which method, declarative or programmatic, to use. This tutorial will focus on the programmatic method of chart creation though all examples work with both approaches. Creating a Simple ChartNow that the fundamental pieces of the charting puzzle have been explained, let’s create a very basic chart. This chart will depict website visits over 3 months time. The chart will be a basic StackedAreas chart with different colors to denote the different months. We’ll take it step by step. The first step is to place our data in the format with which the chart works. The data for our StackedArea chart must hold each value in one array:
The next step is to create the chart programatically.
Then it’s time to add the plot.
Since the numbers in this type of chart are significant, we want to add an X axis (representing days) and a Y axis (representing traffic / views).
Now that the frame of the chart has been created, it is time to add the information for each month. Since we want to fill areas, we’ll need to pass a stroke and fill color within the series:
Now that our chart is built and the data is in place, we can render the chart:
The result is a beautiful chart: Chart Animations, Legends, and ExtrasAs with every other piece of the Dojo library, the Dojo charting library is flexible enough to incorporate other Dojo classes and widgets. The basic chart we created above is nice but could use a few useful enhancements. LegendAdding a legend to the chart is very simple:
Chart AnimationsAs with basic content on a web page, animation adds some flare and shows focus on an element. Each chart type supports different sets of animations. To keep it simple, we can add a “magnify” animation to the markers on our chart so that the marker grows when the mouse enters it:
TooltipsOur chart features markers at the designated x/y axis numbers but we can enhance those markers with tooltips so that the user can see the exact number the marker represents.
You may also pair Dijit themes with your chart to style the tooltip; I’ve added the “tundra” theme’s CSS file to enhance the style of my tooltip. Zooming, Scrolling, and PanningAs you would expect from a vector graphic generation tool, Dojo’s charting library provides a method by which you may flawlessly zoom in and out on any chart. You may also accommodate panning and scrolling on your charts. Read Zooming, Scrolling, and Panning in Dojo Charting to learn more about these features or view a great example of panning, scrolling, and zooming! Chart EventsDojo’s charting library also has its own event connection method: connectToPlot. This method allows you to add event listeners to your chart and trigger functionality based on the given event.
Chart events provide you a wealth of information, including the type of event, coordinates, plot, shape, and more. Visit the Dojo Charting Reference Guide to get detailed information about chart events. Chart ThemesThe Dojo charting library provides over 30 themes to make your charts stand out. You may see a complete list of bundled themes (with preview) in the Dojo Nightly Theme Previewer. SitePen will publish a post in the near future detailing how to create a custom theme! Chart FamiliesThis tutorial showed you how to create a basic, but beautiful 2D chart; Dojo’s charting library has many advanced charting capabilities though. The different families of Dojo charts includes:
New Charting Features in Dojo 1.5The release of Dojo 1.5 introduces new features to the charting library; most notably:
Great Charting ResourcesThe concept of charting can seem difficult, but Dojo’s charting library is a breeze to dive into! |
|
来自: LibraryPKU > 《Web》