Styling Lines, Borders and Fills : Chart « YUI Library « JavaScript DHTML






Styling Lines, Borders and Fills

 

<!--
Software License Agreement (BSD License)
Copyright (c) 2009, Yahoo! Inc.
All rights reserved.

Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the
      following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of Yahoo! Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission of Yahoo! Inc.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sources of Intellectual Property Included in the YUI Library

YUI is issued by Yahoo! under the BSD license above. Below is a list of certain publicly available software that is the source of intellectual property in YUI, along with the licensing terms that pertain to thosesources of IP. This list is for informational purposes only and is not intended to represent an exhaustive list of third party contributions to the YUI.

    * Douglas Crockford's JSON parsing and stringifying methods: In the JSON Utility, Douglas Crockford's JSON parsing and stringifying methods are adapted from work published at JSON.org. The adapted work is in the public domain.
    * Robert Penner's animation-easing algorithms: In the Animation Utility, YUI makes use of Robert Penner's algorithms for easing.
    * Geoff Stearns's SWFObject: In the Charts Control and the Uploader versions through 2.7.0, YUI makes use of Geoff Stearns's SWFObject v1.5 for Flash Player detection and embedding. More information on SWFObject can be found here (http://blog.deconcept.com/swfobject/). SWFObject is (c) 2007 Geoff Stearns and is released under the MIT License (http://www.opensource.org/licenses/mit-license.php).
    * Diego Perini's IEContentLoaded technique: The Event Utility employs a technique developed by Diego Perini and licensed under GPL. YUI's use of this technique is included under our BSD license with the author's permission.

-->

<head>


    <meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Styling Lines, Borders and Fills</title>

<style type="text/css">
/*margin and padding on body element
  can introduce errors in determining
  element position and are not recommended;
  we turn them off as a foundation for YUI
  CSS treatments. */
body {
  margin:0;
  padding:0;
}
</style>

<link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" />
<script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/json/json-min.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/datasource/datasource-min.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/charts/charts-min.js"></script>


<!--begin custom header content for this example-->
<style type="text/css">
  #chart
  {
    width: 500px;
    height: 350px;
  }

  .chart_title
  {
    display: block;
    font-size: 1.2em;
    font-weight: bold;
    margin-bottom: 0.4em;
  }
</style>
<!--end custom header content for this example-->

</head>

<body class=" yui-skin-sam">


<h1>Styling Lines, Borders and Fills</h1>

<div class="exampleIntro">
  <p>The <a href="http://developer.yahoo.com/yui/charts/">YUI Charts Control</a> offers customization through its style and series style objects. This example demonstrates one way to customize a Line Chart.</p>
<p>Please note: The YUI Charts Control requires Flash Player 9.0.45 or higher. The latest version of Flash Player is available at the <a href="http://www.adobe.com/go/getflashplayer">Adobe Flash Player Download Center</a>.</p>


      
</div>

<!--BEGIN SOURCE CODE FOR EXAMPLE  -->

<span class="chart_title">January Daily Profits</span>
<div id="chart">Unable to load Flash content. The YUI Charts Control requires Flash Player 9.0.45 or higher. You can download the latest version of Flash Player from the <a href="http://www.adobe.com/go/getflashplayer">Adobe Flash Player Download Center</a>.</p></div>

<script type="text/javascript">
YAHOO.widget.Chart.SWFURL = "yui_2.7.0b-lib/charts/assets/charts.swf";

//--- data

  //revenue and expenses per day
  var dailyFinancials =
  [
    {date:"1/1/2009", revenue:14002, expenses:15000},
    {date:"1/2/2009", revenue:14027, expenses:16527},
    {date:"1/3/2009", revenue:15083, expenses:17283},
    {date:"1/4/2009", revenue:15006, expenses:18694},
    {date:"1/5/2009", revenue:15035, expenses:18235},
    {date:"1/6/2009", revenue:17871, expenses:18371},
    {date:"1/7/2009", revenue:25887, expenses:17787},
    {date:"1/8/2009", revenue:16149, expenses:16149},
    {date:"1/9/2009", revenue:17366, expenses:15666},
    {date:"1/10/2009", revenue:16424, expenses:15776},
    {date:"1/11/2009", revenue:15817, expenses:18717},
    {date:"1/12/2009", revenue:13554, expenses:18154},
    {date:"1/13/2009", revenue:19782, expenses:18182},
    {date:"1/14/2009", revenue:24400, expenses:18600},
    {date:"1/15/2009", revenue:21780, expenses:18480},
    {date:"1/16/2009", revenue:16681, expenses:21581},
    {date:"1/17/2009", revenue:24645, expenses:21745},
    {date:"1/18/2009", revenue:22212, expenses:20388},
    {date:"1/19/2009", revenue:24026, expenses:24726},
    {date:"1/20/2009", revenue:29264, expenses:24864},
    {date:"1/21/2009", revenue:19922, expenses:23622},
    {date:"1/22/2009", revenue:12373, expenses:18773},
    {date:"1/23/2009", revenue:11944, expenses:18444},
    {date:"1/24/2009", revenue:23741, expenses:18641},
    {date:"1/25/2009", revenue:24758, expenses:19758},
    {date:"1/26/2009", revenue:31611, expenses:19611},
    {date:"1/27/2009", revenue:25612, expenses:18212},
    {date:"1/28/2009", revenue:26830, expenses:18330},
    {date:"1/29/2009", revenue:27929, expenses:19029},
    {date:"1/30/2009", revenue:28153, expenses:19953},
    {date:"1/31/2009", revenue:28018, expenses:18118}
  ];

  //calculate profits from revenue and expenses
  function calculateProfits(dataArray)
  {
    var dataLength = dataArray.length;
    for(var i = 0; i < dataLength; i++)
    {
      var dataObj = dataArray[i];
      dataObj.profits = dataObj.revenue - dataObj.expenses;
    }

    return dataArray;
  }

  var myDataSource = new YAHOO.util.DataSource(calculateProfits(dailyFinancials));
  myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
  myDataSource.responseSchema =
  {
    fields:
    [
      "date",
      "expenses",
      "revenue",
      "profits"
    ]
  };

//--- chart

  //series definition
  var seriesDef = [
    {
      displayName:"Expenses",
      yField:"expenses",
      style:
      {
        lineColor:0xB5BAC8,
        lineAlpha:.5,
        borderColor:0xB5BAC8,
        fillColor:0xB5BAC8
      }
    },
    {
      displayName:"Revenue",
      yField:"revenue",
      style:
      {
        lineColor:0xB5BAC8,
        lineAlpha:.5,
        borderColor:0xB5BAC8,
        fillColor:0xffffff
      }
    },
    {
      displayName:"Profits",
      yField:"profits",
      style:
      {
        lineColor:0x79839B,
        borderColor:0x79839B,
        fillColor:0x79839B
      }
    }
  ];

  //Style object
  var styleDef =
  {
    xAxis:
    {
      majorTicks:
      {
        display:"inside",
        length:3,
        size:1
      },
      minorTicks:
      {
        display:"inside",
        length:2
      },
      labelRotation: -90
    },
    yAxis:
    {
      zeroGridLine:
      {
        size:2,
        color:0xff0000
      },
      minorTicks:{display:"none"}
    }
  }

  //format date labels
  YAHOO.example.formatTimeData = function(value, major)
  {
    var formattedData = YAHOO.util.Date.format(new Date(value), {format:"%b %e"});
    return formattedData.toString();
  }

  //format currency labels
  YAHOO.example.formatCurrencyAxisLabel = function( value )
  {
    return YAHOO.util.Number.format( value,
    {
      prefix: "$",
      thousandsSeparator: ",",
      decimalPlaces: 2
    });
  }

  //DataTip function for the chart
  YAHOO.example.formatDataTipText = function(item, index, series)
  {
    var str = series.displayName + " for " + item.date;
    str += "\n" + YAHOO.example.formatCurrencyAxisLabel(item[(series.displayName).toLowerCase()]);
    return str;
  }

  //Create a TimeAxis for displaying dates
  var myTimeAxis = new YAHOO.widget.TimeAxis();
  myTimeAxis.labelFunction = YAHOO.example.formatTimeData;
  myTimeAxis.majorTimeUnit = "day";

  //create a Numeric Axis for displaying dollars
  myCurrencyAxis = new YAHOO.widget.NumericAxis();
  myCurrencyAxis.labelFunction = YAHOO.example.formatCurrencyAxisLabel;

  //create a Chart
  var mychart = new YAHOO.widget.LineChart("chart", myDataSource,
  {
    series: seriesDef,
    style: styleDef,
    xField: "date",
    xAxis: myTimeAxis,
    yAxis: myCurrencyAxis,
    dataTipFunction:YAHOO.example.formatDataTipText,
    //only needed for flash player express install
    expressInstall: "yui_2.7.0b-assets/charts-assets/expressinstall.swf"
  });

</script>
<!--END SOURCE CODE FOR EXAMPLE  -->

</body>

   
  








yui_2.7.0b.zip( 4,431 k)

Related examples in the same category

1.Chart and DataTable Example
2.Chart with Legend Example
3.Charts Quickstart Example
4.Column Chart with Rotated Title and Labels
5.Skinning a Chart
6.Stacked Bar Chart
7.Charts in a TabView
8.Chart with DataSource Polling