98 |
- |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
PHPlot / contrib / color_range
|
|
|
4 |
$Id: color_range.php 974 2011-07-26 17:20:50Z lbayuk $
|
|
|
5 |
PHPlot contrib code - public domain - no copyright - use as you wish
|
|
|
6 |
|
|
|
7 |
Original contribution from: Josep Sanz <josep dot sans at w3 dot es>
|
|
|
8 |
"I wrote this code to calculate the range of colors between 2 colors
|
|
|
9 |
to plot using the range from color_a to color_b..."
|
|
|
10 |
|
|
|
11 |
I have changed the code and repackaged it, but the idea is the same.
|
|
|
12 |
Given 2 colors and number of data sets, computes an array of colors
|
|
|
13 |
that make up a gradient between the two provided colors, for use
|
|
|
14 |
with SetDataColors().
|
|
|
15 |
|
|
|
16 |
Provides the following functions:
|
|
|
17 |
$colors = color_range($color_a, $color_b, $n_intervals)
|
|
|
18 |
Returns a color array for SetDataColors.
|
|
|
19 |
|
|
|
20 |
$n = count_data_sets($data, $data_type)
|
|
|
21 |
Counts the number of data sets in a data array.
|
|
|
22 |
This can be used to provide $n_intervals in color_range().
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
/*
|
|
|
28 |
Fill a color map with a gradient step between two colors.
|
|
|
29 |
Arguments:
|
|
|
30 |
$color_a : Starting color for the gradient. Array of (r, g, b)
|
|
|
31 |
$color_b : Ending color for the gradient. Array of (r, g, b)
|
|
|
32 |
$n_steps : Total number of color steps, including color_a and color_b.
|
|
|
33 |
|
|
|
34 |
Returns: A color map array with n_steps colors in the form
|
|
|
35 |
$colors[i][3], suitable for SetDataColors().
|
|
|
36 |
|
|
|
37 |
Notes:
|
|
|
38 |
You may use the PHPlot internal function $plot->SetRGBColor($color)
|
|
|
39 |
to convert a color name or #rrggbb notation into the required array
|
|
|
40 |
of 3 values (r, g, b) for color_a and color_b.
|
|
|
41 |
|
|
|
42 |
Newer versions of PHPlot use 4 components (r, g, b, a) arrays for color.
|
|
|
43 |
This script ignores the alpha component in those arrays.
|
|
|
44 |
|
|
|
45 |
*/
|
|
|
46 |
function color_range($color_a, $color_b, $n_steps)
|
|
|
47 |
{
|
|
|
48 |
if ($n_steps < 2) $n_steps = 2;
|
|
|
49 |
$nc = $n_steps - 1;
|
|
|
50 |
# Note: $delta[] and $current[] are kept as floats. $colors is integers.
|
|
|
51 |
for ($i = 0; $i < 3; $i++)
|
|
|
52 |
$delta[$i] = ($color_b[$i] - $color_a[$i]) / $nc;
|
|
|
53 |
$current = $color_a;
|
|
|
54 |
for ($col = 0; $col < $nc; $col++) {
|
|
|
55 |
for ($i = 0; $i < 3; $i++) {
|
|
|
56 |
$colors[$col][$i] = (int)$current[$i];
|
|
|
57 |
$current[$i] += $delta[$i];
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
$colors[$nc] = $color_b; # Make sure the last color is exact.
|
|
|
61 |
return $colors;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
/*
|
|
|
66 |
Determine the number of data sets (plot lines, bars per group, pie
|
|
|
67 |
segments, etc.) contained in a data array.
|
|
|
68 |
This can be used to determine n_steps for $color_range.
|
|
|
69 |
|
|
|
70 |
Arguments:
|
|
|
71 |
$data : PHPlot data array
|
|
|
72 |
$data_type : PHPlot data type, describing $data. (e.g. 'data-data')
|
|
|
73 |
Returns: The number of data sets in the data array.
|
|
|
74 |
Notes:
|
|
|
75 |
This has to scan the entire data array. Don't use this unless you
|
|
|
76 |
really don't have a better way to determine the number of data sets.
|
|
|
77 |
|
|
|
78 |
This does NOT require that the data array be integer indexed.
|
|
|
79 |
|
|
|
80 |
*/
|
|
|
81 |
function count_data_sets($data, $data_type)
|
|
|
82 |
{
|
|
|
83 |
|
|
|
84 |
if ($data_type == 'text-data-single')
|
|
|
85 |
return count($data); # Pie chart, 1 segment per record
|
|
|
86 |
|
|
|
87 |
# Get the longest data record:
|
|
|
88 |
$max_row = 0;
|
|
|
89 |
foreach ($data as $row)
|
|
|
90 |
if (($n = count($row)) > $max_row) $max_row = $n;
|
|
|
91 |
|
|
|
92 |
if ($data_type == 'text-data' || $data_type == 'text-data-yx')
|
|
|
93 |
return ($max_row - 1); # Each record is (label Y1 Y2...)
|
|
|
94 |
|
|
|
95 |
if ($data_type == 'data-data' || $data_type == 'data-data-yx')
|
|
|
96 |
return ($max_row - 2); # Each record is (label X Y1 Y2...)
|
|
|
97 |
|
|
|
98 |
if ($data_type == 'data-data-error')
|
|
|
99 |
return (($max_row - 2) / 3); # Each record is (label X Y1 Y1+ Y1-...)
|
|
|
100 |
|
|
|
101 |
# Not a recognized data type... Just return something sane.
|
|
|
102 |
return $max_row;
|
|
|
103 |
}
|