"D3.js Advanced Series-9.0" Interactive Prompt box

Source: Internet
Author: User

In general, there is not much text in the chart. However, sometimes you need some text to describe some graphical elements. Then, you can implement an interaction: when the user mouse slides to a graphic element, a prompt box appears with descriptive text. This is a simple, universal interaction that works on almost all charts. By customizing the appearance to the cue box, you can give the user a good experience.


1. How to make a prompt box

The Prompt box is "text" Plus "border". display text, generally with SVG's <text>, but there are two problems:

    • If the string is too long, the,<text> element cannot wrap, although it can be difficult to simulate the function of line wrapping by <text> 's child element <tspan>.
    • <text> is the element of SVG. This means that,<text> is "graphics" rather than "text", which is essentially the same as the <circle>, <line>, <path>, and so on in SVG. Then, when outputting SVG graphics,,<text> also outputs as part of the graph.

Therefore, the <text> elements of SVG are not suitable for making prompt boxes.

There is an easy way to do this: div + CSS. A div is an element of HTML that sets its positioning method to absolute positioning in a style:

. Tooltip{position:absolute;width:120;height:auto;}

Then, when the monitor hears the mouse event, uses the mouse coordinates as the prompt box to locate, the Code shape is as follows:

Element.on ("MouseOver", function (d) {Tooltip.style ("left", (D3.event.pageX) + "px"). Style ("Top", (D3.event.pageY) + " px ");})

In practice, to make the cue box beautiful, you also need to set more styles for the Div.


2. Add a hint box to a pie chart

Take a pie chart as an example and draw a good pie chart.

First, add a <div> in <body> and set the transparency to 0, which is completely transparent, and the Div class is set to ToolTip.

var tooltip = D3.select ("Body"). Append ("div"). attr ("Class", "ToolTip"). Style ("opacity", 0.0);

Then, define a tooltip style and set its positioning method to absolute positioning , this step is the focus. Other properties when it comes to the appearance of the border and how the text is displayed are defined here as a simple.

. Tooltip{position:absolute;width:120;height:auto;font-family:simsun;font-size:14px;text-align:center; Border-style:solid; border-width:1px;background-color:white;border-radius:5px;}

Finally, customize the listener for mouse events for each graphical element of the pie chart, including: When the mouse is placed on the graph (mouseover), when the mouse moves on the graph (MouseMove), when the mouse moves out (mouseout).

Arcs.on ("MouseOver", function (d) {/* mouse move in, (1) Change the text of the Cue box by Selection.html () (2) by changing the style left and top To set the position of the cue box (3) Set the transparency of the Cue box to 1.0 (completely opaque) */tooltip.html (D.data[0] + "shipments are" + "<br/>" +   d.data[1] + "million units"). Style (" Left ", (D3.event.pageX) +" px "). Style (" Top ", (D3.event.pageY +) +" px "). Style (" opacity ", 1.0);}). On ("MouseMove", function (d) {/* * When the mouse moves, change the style left and top to change the position of the cue box */tooltip.style ("Left", (D3.event.pageX) + "px"). Style (" Top ", (D3.event.pageY +) +" px "). On ("Mouseout", function (d) {/* * When the mouse moves out, the transparency is set to 0.0 (fully transparent) */tooltip.style ("opacity", 0.0);})

D3.event.pageX and D3.event.pageY are the coordinates of the current mouse relative to the browser page, while the left and top styles are relative to the browser page for <div> elements that are in an absolutely positioned state. When assigning a value, the value of top is D3.event.pageY + 20, so that the cue box appears slightly below the mouse position, which prevents the mouse from moving on the cue box, causing the issue to not trigger the event.


3. Results

As shown, a prompt box appears when you move the mouse over "Lenovo."

Source code please click on the following link, the message to view the source code:

Http://www.ourd3js.com/demo/G-9.0/tooltip.html

Thank you for reading.

Document Information
    • Copyright Notice: Attribution (by)-Non-commercial (NC)-No deduction (ND)
    • Published: June 15, 2015
    • More content: our D3. JS-Data Visualization special station and CSDN personal blog
    • Remark: This article is published in our D3. JS, reproduced please indicate the source, thank you

"D3.js Advanced Series-9.0" Interactive Prompt box

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.