CssÊý¾Ýͼ±í
Data visualization is mostly achieved with flash applications or
with help of some programming languages. Are those solutions the only
way to present, let's say simple data chart? How about giving it a try
with nothing but good ol' css?
Take a look at the Demo
| Download Css Chart
Approach
In this example I am not using JavaScript or any backend application. All I rely on is well formed markup and css.
So, the goal is to present data chart. We could say that a chart is a
two dimensional object. So, the best structural and semantical choice
is definition list. Why? Well, for starter, it is a list of items.
Although the list is linear, we could interpret definition titles (dt
elements) as items on x axis and definition descriptions (dd elements)
as values on y axis.
If that doesn't make sense because of my poor explanation skills :) what I'll do here is turn this:
into this
with css alone.
The markup
In my example I have used a period of last 12 days and presented my working energy level in percentages,
100% being the best I've felt about working ever. As you can see, on 6th day it went up a bit because I
received a solid paycheck and that boosted me up :)
Anyway, to structure this kind of data I chose definition list. Definition title contains the day title
<dt>Day 1</dt>
And definition description contains the value
<dd>36</dd>
Inside the definition description element I will add a span and nested em element. Not for structural purposes,
but to give myself enough elements to work with. I will also assign classes that I can use later on.
So definition description looks like this now:
<dd><span class="type2 p80"><em>80</em></span></dd>
Styling it
Definition list contains acts as a chart container, so I'll assign background image to it and set the width and
height to match the dimension of the chart. Also, I'll remove default margins and paddings
dl#csschart
Ïà¹ØÎĵµ£º
½ñÌì¿´µ½Ò»ÆªÎÄÕÂÊÇcss´òÔì½ø¶ÈÌõ¡£È»ºó×Ô¼º×ö³É¶¯Ì¬µÄ½ø¶ÈÌõ£¬Í¬Ñù£¬Ê¹Óõ½cssºÍjauery£¬Çë¿´£º
1 Ê×ÏÈ£¬ÐèÒªÒýÈëjquery.jsÎļþ¡£
<script src="Content/jquery.js" type="text/javascript"></script>
2 ËùÓõ½µÄÑùʽ²¿·Ö£º
<style>
.graph {
& ......
IE6ÏÂĬÈϵÄ×ÖÌå³ß´ç´óÖÂÔÚ 12 – 14px Ö®¼ä£¬µ±ÄãÊÔͼ¶¨ÒåÒ»¸ö¸ß¶ÈСÓÚÕâ¸öĬÈÏÖµµÄ div µÄʱºò£¬ IE »á¹ÌÖ´µÄÈÏΪÕâ¸ö²ãµÄ¸ß¶È²»Ó¦¸ÃСÓÚ×ÖÌåµÄÐиߡ£ËùÒÔ¼´Ê¹ÄãÓà height:4px; À´¶¨ÒåÁËÒ»¸ö div µÄ¸ß¶È£¬Êµ¼ÊÔÚ IE ÏÂÏÔʾµÄÈÔÈ»ÊÇÒ»¸ö 12 px ×óÓҸ߶ȵIJ㡣Ìí¼Óoverflow: hidden½â¾öÎÊÌâ¡£
<div style=&rdquo ......
½ñÌìдÁËÒ»¸ö¼òµ¥µÄÍøÒ³£¬Çë¿´´úÂ룺
´íÎóµÄ£º
HTML´úÂ룺
½ñÌìµÄÈÕ×Ó
//CSS´úÂëʹÓÃÍⲿÒýÈëʽ
ÎҵļÒÏçÔÚ¿Àï
CSS´úÂ룺
/* CSS Document */
body{
font-family:Arial, Helvetica, sans-serif;
font-size:20px;
color:red; /*ÒªµÄ¾ÍÊÇÕâÑùµÄЧ¹û£ºÉèÖÃ×ÖÌåÑÕɫΪºìÉ«*/
}
½á¹ûÏÔʾµÄ×ÖÌ ......
ÔÀí£º
µ±Á½¸öͬ¸¸ÔªËØz-indexÏàͬʱ£¬ºóÃæµÄÔªËØÏÔʾÔÚÉÏÃæ¡£
µ±z-index³¬¹ýä¯ÀÀÆ÷×î´óֵʱ£¬ä¯ÀÀÆ÷»á°´×î´óÖµ¼ÆËã¡£
·½·¨£º
Á¬Ðø·ÅÖÃÁ½¸ödivÔªËØd1£¬d2£»²¢É趨position:absolute¼°²»Í¬±³¾°É«¡£
É趨d1.z-index = max + 1, d2.z-index = max£»
µ±max + 1ÓÐЧʱ£¬d1ÔÚÇ°£»µ±max + 1 ÎÞЧʱ£¬d2ÔÚÇ°¡££¨²Î¿´ÔÀí£© ......
/* Cascading Style Sheet for IE4.01 last updated 10-16-97 */
DIV {COLOR: #333333; FONT-FAMILY: "ËÎÌå", "Arial", "Tahoma"; FONT-SIZE: 9pt}
P {COLOR: #333333; FONT-FAMILY: "ËÎÌå", "Arial", "Tahoma"; FONT-SIZE: 9pt}
TD {COLOR: #333333; FONT-FAMILY: "ËÎÌå", "Arial", "Tahoma"; FONT-SIZE: 9pt}
BODY {C ......