📄 zoomscrolldemo.php
字号:
#================================================================================
# Step 1 - Configure overall chart appearance.
#================================================================================
# Create an XYChart object 600 x 300 pixels in size, with pale blue (0xf0f0ff) background, black
# (000000) rounded border, 1 pixel raised effect.
$c = new XYChart(600, 300, 0xf0f0ff, 0x000000);
$c->setRoundedFrame();
# Set the plotarea at (52, 60) and of size 520 x 192 pixels. Use white (ffffff) background.
# Enable both horizontal and vertical grids by setting their colors to grey (cccccc). Set
# clipping mode to clip the data lines to the plot area.
$c->setPlotArea(55, 60, 520, 192, 0xffffff, -1, -1, 0xcccccc, 0xcccccc);
$c->setClipping();
# Add a top title to the chart using 15 pts Times New Roman Bold Italic font, with a light blue
# (ccccff) background, black (000000) border, and a glass like raised effect.
$textBoxObj = $c->addTitle("Zooming and Scrolling Demonstration", "timesbi.ttf", 15);
$textBoxObj->setBackground(0xccccff, 0x000000, glassEffect());
# Add a bottom title to the chart to show the date range of the axis, with a light blue (ccccff)
# background.
$textBoxObj = $c->addTitle2(Bottom, sprintf(
"From <*font=arialbi.ttf*>%s<*/font*> to <*font=arialbi.ttf*>%s<*/font*> (Duration ".
"<*font=arialbi.ttf*>%s<*/font*> days)", $c->formatValue($viewPortStartDate,
"{value|mmm dd, yyyy}"), $c->formatValue($viewPortEndDate, "{value|mmm dd, yyyy}"), (int)(
0.5 + ($viewPortEndDate - $viewPortStartDate) / 86400)), "ariali.ttf", 10);
$textBoxObj->setBackground(0xccccff);
# Add a legend box at the top of the plot area with 9pts Arial Bold font with flow layout.
$legendObj = $c->addLegend(50, 33, false, "arialbd.ttf", 9);
$legendObj->setBackground(Transparent, Transparent);
# Set axes width to 2 pixels
$c->xAxis->setWidth(2);
$c->yAxis->setWidth(2);
# Add a title to the y-axis
$c->yAxis->setTitle("Price (USD)", "arialbd.ttf", 10);
#================================================================================
# Step 2 - Add data to chart
#================================================================================
#
# In this example, we represent the data by lines. You may modify the code below if you want to
# use other representations (areas, scatter plot, etc).
#
# Add a line layer for the lines, using a line width of 2 pixels
$layer = $c->addLineLayer2();
$layer->setLineWidth(2);
# Now we add the 3 data series to a line layer, using the color red (ff0000), green (00cc00) and
# blue (0000ff)
$layer->setXData($timeStamps);
$layer->addDataSet($dataSeriesA, 0xff0000, "Product Alpha");
$layer->addDataSet($dataSeriesB, 0x00cc00, "Product Beta");
$layer->addDataSet($dataSeriesC, 0x0000ff, "Product Gamma");
#================================================================================
# Step 3 - Set up x-axis scale
#================================================================================
# Set x-axis date scale to the view port date range. ChartDirector auto-scaling will
# automatically determine the ticks on the axis.
$c->xAxis->setDateScale($viewPortStartDate, $viewPortEndDate);
#
# In the current demo, the x-axis range can be from a few years to a few days. We can let
# ChartDirector auto-determine the date/time format. However, for more beautiful formatting, we
# set up several label formats to be applied at different conditions.
#
# If all ticks are yearly aligned, then we use "yyyy" as the label format.
$c->xAxis->setFormatCondition("align", 31104000);
$c->xAxis->setLabelFormat("{value|yyyy}");
# If all ticks are monthly aligned, then we use "mmm yyyy" in bold font as the first label of a
# year, and "mmm" for other labels.
$c->xAxis->setFormatCondition("align", 2592000);
$c->xAxis->setMultiFormat(StartOfYearFilter(), "<*font=bold*>{value|mmm yyyy}", AllPassFilter(),
"{value|mmm}");
# If all ticks are daily algined, then we use "mmm dd<*br*>yyyy" in bold font as the first label
# of a year, and "mmm dd" in bold font as the first label of a month, and "dd" for other labels.
$c->xAxis->setFormatCondition("align", 86400);
$c->xAxis->setMultiFormat(StartOfYearFilter(),
"<*block,halign=left*><*font=bold*>{value|mmm dd<*br*>yyyy}", StartOfMonthFilter(),
"<*font=bold*>{value|mmm dd}");
$c->xAxis->setMultiFormat2(AllPassFilter(), "{value|dd}");
# For all other cases (sub-daily ticks), use "hh:nn<*br*>mmm dd" for the first label of a day,
# and "hh:nn" for other labels.
$c->xAxis->setFormatCondition("else");
$c->xAxis->setMultiFormat(StartOfDayFilter(), "<*font=bold*>{value|hh:nn<*br*>mmm dd}",
AllPassFilter(), "{value|hh:nn}");
#================================================================================
# Step 4 - Set up y-axis scale
#================================================================================
if ($viewer->getZoomDirection() == DirectionHorizontal) {
# y-axis is auto-scaled - so vertically, the view port always cover the entire y data range.
# We save the y-axis scale for supporting xy-zoom mode if needed in the future.
$c->layout();
$yAxisObj = $c->yAxis;
$viewer->setCustomAttr("minValue", $yAxisObj->getMinValue());
$yAxisObj = $c->yAxis;
$viewer->setCustomAttr("maxValue", $yAxisObj->getMaxValue());
$viewer->setViewPortTop(0);
$viewer->setViewPortHeight(1);
} else {
# xy-zoom mode - retrieve the auto-scaled axis range, which contains the entire y data
# range.
$minValue = $viewer->getCustomAttr("minValue");
$maxValue = $viewer->getCustomAttr("maxValue");
# Compute the view port axis range
$axisLowerLimit = $maxValue - ($maxValue - $minValue) * ($viewer->getViewPortTop() +
$viewer->getViewPortHeight());
$axisUpperLimit = $maxValue - ($maxValue - $minValue) * $viewer->getViewPortTop();
# Set the axis scale to the view port axis range
$c->yAxis->setLinearScale($axisLowerLimit, $axisUpperLimit);
# By default, ChartDirector will round the axis scale to the tick position. For zooming, we
# want to use the exact computed axis scale and so we disable rounding.
$c->yAxis->setRounding(false, false);
}
#================================================================================
# Step 5 - Output the chart
#================================================================================
# Create the image and save it in a temporary location
$chartQuery = $c->makeSession($viewer->getId());
# Include tool tip for the chart
$imageMap = $c->getHTMLImageMap("", "",
"title='[{dataSetName}] {x|mmm dd, yyyy}: USD {value|2}'");
# Set the chart URL, image map, and chart metrics to the viewer. For the image map, we use
# delayed delivery and with compression, so the chart image will show up quicker.
$viewer->setImageUrl("getchart.php?".$chartQuery);
$viewer->setImageMap("getchart.php?".$viewer->makeDelayedMap($imageMap, true));
$viewer->setChartMetrics($c->getChartMetrics());
}
#/ <summary>
#/ A utility to create the <option> tags for the date range <select> boxes
#/ </summary>
#/ <param name="startValue">The minimum selectable value.</param>
#/ <param name="endValue">The maximum selectable value.</param>
#/ <param name="selectedValue">The currently selected value.</param>
function createSelectOptions($startValue, $endValue, $selectedValue) {
$ret = array_pad(array(), ($endValue - $startValue + 1), null);
for($i = $startValue; $i < $endValue + 1; ++$i) {
if ($i == $selectedValue) {
# Use a "selected" <option> tag if it is the selected value
$ret[$i - $startValue] = "<option value='$i' selected>$i</option>";
} else {
# Use a normal <option> tag
$ret[$i - $startValue] = "<option value='$i'>$i</option>";
}
}
return join("", $ret);
}
# Create the WebChartViewer object
$viewer = new WebChartViewer("chart1");
if ($viewer->isPartialUpdateRequest()) {
# Is a partial update request (AJAX chart update)
processPartialUpdate($viewer);
# Since it is a partial update, there is no need to output the entire web page. We stream the
# chart and then terminate the script immediately.
print($viewer->partialUpdateChart());
exit();
} else if ($viewer->isFullUpdateRequest()) {
# Is a full update request
processFullUpdate($viewer);
} else {
# Is a initial request
createFirstChart($viewer);
}
# Create the <option> tags for the start date / end date select boxes to reflect the currently
# selected data range
$startYearSelectOptions = createSelectOptions((int)(getChartYMD($startDate) / 10000), (int)(
getChartYMD($endDate) / 10000), (int)(getChartYMD($viewPortStartDate) / 10000));
$startMonthSelectOptions = createSelectOptions(1, 12, (int)(getChartYMD($viewPortStartDate) / 100) %
100);
$startDaySelectOptions = createSelectOptions(1, 31, (int)(getChartYMD($viewPortStartDate) % 100));
$endYearSelectOptions = createSelectOptions((int)(getChartYMD($startDate) / 10000), (int)(
getChartYMD($endDate) / 10000), (int)(getChartYMD($viewPortEndDate) / 10000));
$endMonthSelectOptions = createSelectOptions(1, 12, (int)(getChartYMD($viewPortEndDate) / 100) % 100
);
$endDaySelectOptions = createSelectOptions(1, 31, (int)(getChartYMD($viewPortEndDate) % 100));
?>
<html>
<head>
<title>ChartDirector Zoom and Scroll Demonstration</title>
<script language="Javascript" src="cdjcv.js"></script>
<style>
div.chartPushButtonSelected { padding:5px; background:#ccffcc; cursor:hand; }
div.chartPushButton { padding:5px; cursor:hand; }
td.chartPushButton { font-family:Verdana; font-size:9pt; cursor:pointer; border-bottom:#000000 1px solid; }
</style>
</head>
<body leftMargin="0" topMargin="0" onload="initJsChartViewer()">
<script>
// Initialize browser side Javascript controls
function initJsChartViewer()
{
// Check if the Javascript ChartViewer library is loaded
if (!window.JsChartViewer)
return;
// Get the Javascript ChartViewer object
var viewer = JsChartViewer.get('<?php echo $viewer->getId()?>');
// Connect the mouse usage buttons to the Javascript ChartViewer object
connectViewerMouseUsage('ViewerMouseUsage1', viewer);
// Connect the xy zoom mode buttons to the Javascript ChartViewer object
connectViewerZoomControl('ViewerZoomControl1', viewer);
// Detect if browser is capable of support partial update (AJAX chart update)
if (JsChartViewer.canSupportPartialUpdate())
{
// Browser can support partial update, so connect the view port change event and
// the submit button to trigger a partial update
viewer.attachHandler("ViewPortChanged", viewer.partialUpdate);
document.getElementById('SubmitButton').onclick = function() { viewer.partialUpdate(); return false; };
// For partial updates, we need to pass the start date / end date select boxes values to/from
// the server via Javascript ChartViewer custom attributes
var controlsToSync = ['StartYear', 'StartMonth', 'StartDay', 'EndYear', 'EndMonth', 'EndDay'];
viewer.attachHandler("PreUpdate", function() { copyToViewer(viewer, controlsToSync); });
viewer.attachHandler("PostUpdate", function() { copyFromViewer(viewer, controlsToSync); });
}
else
// Browser cannot support partial update - so use full page update
viewer.attachHandler("ViewPortChanged", function() { document.forms[0].submit(); });
}
// A utility to copy HTML control values to Javascript ChartViewer custom attributes
function copyToViewer(viewer, controlsToSync)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -