⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 financechart.php

📁 一个绝对棒的报表绘图软件
💻 PHP
📖 第 1 页 / 共 5 页
字号:
        }
    }

    #/ <summary>
    #/ Get the tool tip format for display date/time
    #/ </summary>
    #/ <returns>The tool tip format string.</returns>
    function getToolTipDateFormat() {
        if ($this->m_timeStamps == null) {
            return $this->m_toolTipHourFormat;
        }
        if (count($this->m_timeStamps) <= $this->m_extraPoints) {
            return $this->m_toolTipHourFormat;
        }
        $resolution = ($this->m_timeStamps[count($this->m_timeStamps) - 1] - $this->m_timeStamps[0])
             / count($this->m_timeStamps);
        if ($resolution >= 30 * 86400) {
            return $this->m_toolTipMonthFormat;
        } else if ($resolution >= 86400) {
            return $this->m_toolTipDayFormat;
        } else {
            return $this->m_toolTipHourFormat;
        }
    }

    #/ <summary>
    #/ Set the number format for use in displaying values in legend keys and tool tips.
    #/ </summary>
    #/ <param name="formatString">The default number format.</param>
    function setNumberLabelFormat($formatString) {
        if ($formatString != null) {
            $this->m_generalFormat = $formatString;
        }
    }

    #/ <summary>
    #/ A utility function to compute triangular moving averages
    #/ </summary>
    #/ <param name="data">An array of numbers as input.</param>
    #/ <param name="period">The moving average period.</param>
    #/ <returns>An array representing the triangular moving average of the input array.</returns>
    function computeTriMovingAvg($data, $period) {
        $p = $period / 2 + 1;
        $tmpArrayMath1 = new ArrayMath($data);
        $tmpArrayMath1->movAvg($p);
        $tmpArrayMath1->movAvg($p);
        return $tmpArrayMath1->result();
    }

    #/ <summary>
    #/ A utility function to compute weighted moving averages
    #/ </summary>
    #/ <param name="data">An array of numbers as input.</param>
    #/ <param name="period">The moving average period.</param>
    #/ <returns>An array representing the weighted moving average of the input array.</returns>
    function computeWeightedMovingAvg($data, $period) {
        $acc = new ArrayMath($data);
        for($i = 2; $i < $period + 1; ++$i) {
            $tmpArrayMath1 = new ArrayMath($data);
            $tmpArrayMath1->movAvg($i);
            $tmpArrayMath1->mul($i);
            $acc->add($tmpArrayMath1->result());
        }
        $divObj = $acc->div((1 + $period) * $period / 2);
        return $divObj->result();
    }

    #/ <summary>
    #/ A utility function to obtain the last valid position (that is, position not
    #/ containing cd.NoValue) of a data series.
    #/ </summary>
    #/ <param name="data">An array of numbers as input.</param>
    #/ <returns>The last valid position in the input array, or -1 if all positions
    #/ are cd.NoValue.</returns>
    function lastIndex($data) {
        $i = count($data) - 1;
        while ($i >= 0) {
            if ($data[$i] != NoValue) {
                break;
            }
            $i = $i - 1;
        }
        return $i;
    }

    #/ <summary>
    #/ Set the data used in the chart. If some of the data are not available, some artifical
    #/ values should be used. For example, if the high and low values are not available, you
    #/ may use closeData as highData and lowData.
    #/ </summary>
    #/ <param name="timeStamps">An array of dates/times for the time intervals.</param>
    #/ <param name="highData">The high values in the time intervals.</param>
    #/ <param name="lowData">The low values in the time intervals.</param>
    #/ <param name="openData">The open values in the time intervals.</param>
    #/ <param name="closeData">The close values in the time intervals.</param>
    #/ <param name="volData">The volume values in the time intervals.</param>
    #/ <param name="extraPoints">The number of leading time intervals that are not
    #/ displayed in the chart. These intervals are typically used for computing
    #/ indicators that require extra leading data, such as moving averages.</param>
    function setData($timeStamps, $highData, $lowData, $openData, $closeData, $volData, $extraPoints
        ) {
        $this->m_timeStamps = $timeStamps;
        $this->m_highData = $highData;
        $this->m_lowData = $lowData;
        $this->m_openData = $openData;
        $this->m_closeData = $closeData;
        if ($extraPoints > 0) {
            $this->m_extraPoints = $extraPoints;
        } else {
            $this->m_extraPoints = 0;
        }

        #///////////////////////////////////////////////////////////////////////
        # Auto-detect volume units
        #///////////////////////////////////////////////////////////////////////
        $tmpArrayMath1 = new ArrayMath($volData);
        $maxVol = $tmpArrayMath1->max();
        $units = array("", "K", "M", "B");
        $unitIndex = count($units) - 1;
        while (($unitIndex > 0) && ($maxVol < pow(1000, $unitIndex))) {
            $unitIndex = $unitIndex - 1;
        }

        $tmpArrayMath1 = new ArrayMath($volData);
        $tmpArrayMath1->div(pow(1000, $unitIndex));
        $this->m_volData = $tmpArrayMath1->result();
        $this->m_volUnit = $units[$unitIndex];
    }

    #////////////////////////////////////////////////////////////////////////////
    # Format x-axis labels
    #////////////////////////////////////////////////////////////////////////////
    function setXLabels(&$a) {
        $a->setLabels2($this->m_timeStamps);
        if ($this->m_extraPoints < count($this->m_timeStamps)) {
            $tickStep = (int)((count($this->m_timeStamps) - $this->m_extraPoints) *
                $this->m_timeLabelSpacing / ($this->m_totalWidth - $this->m_leftMargin -
                $this->m_rightMargin)) + 1;
            $timeRangeInSeconds = $this->m_timeStamps[count($this->m_timeStamps) - 1] -
                $this->m_timeStamps[$this->m_extraPoints];
            $secondsBetweenTicks = $timeRangeInSeconds / ($this->m_totalWidth - $this->m_leftMargin
                 - $this->m_rightMargin) * $this->m_timeLabelSpacing;

            if ($secondsBetweenTicks * (count($this->m_timeStamps) - $this->m_extraPoints) <=
                $timeRangeInSeconds) {
                $tickStep = 1;
                if (count($this->m_timeStamps) > 1) {
                    $secondsBetweenTicks = $this->m_timeStamps[count($this->m_timeStamps) - 1] -
                        $this->m_timeStamps[count($this->m_timeStamps) - 2];
                } else {
                    $secondsBetweenTicks = 86400;
                }
            }

            if (($secondsBetweenTicks > 360 * 86400) || (($secondsBetweenTicks > 90 * 86400) && (
                $timeRangeInSeconds >= 720 * 86400))) {
                #yearly ticks
                $a->setMultiFormat2(StartOfYearFilter(), $this->m_yearFormat, $tickStep);
            } else if (($secondsBetweenTicks >= 30 * 86400) || (($secondsBetweenTicks > 7 * 86400)
                 && ($timeRangeInSeconds >= 60 * 86400))) {
                #monthly ticks
                $monthBetweenTicks = (int)($secondsBetweenTicks / 31 / 86400) + 1;
                $a->setMultiFormat(StartOfYearFilter(), $this->m_firstMonthFormat,
                    StartOfMonthFilter($monthBetweenTicks), $this->m_otherMonthFormat);
                $a->setMultiFormat2(StartOfMonthFilter(), "-", 1, false);
            } else if (($secondsBetweenTicks >= 86400) || (($secondsBetweenTicks > 6 * 3600) && (
                $timeRangeInSeconds >= 86400))) {
                #daily ticks
                $a->setMultiFormat(StartOfMonthFilter(), $this->m_firstDayFormat, StartOfDayFilter(
                    1, 0.5), $this->m_otherDayFormat, $tickStep);
            } else {
                #hourly ticks
                $a->setMultiFormat(StartOfDayFilter(1, 0.5), $this->m_firstHourFormat,
                    StartOfHourFilter(1, 0.5), $this->m_otherHourFormat, $tickStep);
            }
        }
    }

    #////////////////////////////////////////////////////////////////////////////
    # Create tool tip format string for showing OHLC data
    #////////////////////////////////////////////////////////////////////////////
    function getHLOCToolTipFormat() {
        return sprintf("title='%s Op:{open|%s}, Hi:{high|%s}, Lo:{low|%s}, Cl:{close|%s}'",
            $this->getToolTipDateFormat(), $this->m_generalFormat, $this->m_generalFormat,
            $this->m_generalFormat, $this->m_generalFormat);
    }

    #/ <summary>
    #/ Add the main chart - the chart that shows the HLOC data.
    #/ </summary>
    #/ <param name="height">The height of the main chart in pixels.</param>
    #/ <returns>An XYChart object representing the main chart created.</returns>
    function addMainChart($height) {
        $this->m_mainChart = $this->addIndicator($height);
        $this->setMainChart($this->m_mainChart);
        $this->m_mainChart->yAxis->setMargin(2 * $this->m_yAxisMargin);
        if ($this->m_logScale) {
            $this->m_mainChart->yAxis->setLogScale();
        } else {
            $this->m_mainChart->yAxis->setLinearScale();
        }
        return $this->m_mainChart;
    }

    #/ <summary>
    #/ Add a candlestick layer to the main chart.
    #/ </summary>
    #/ <param name="upColor">The candle color for an up day.</param>
    #/ <param name="downColor">The candle color for a down day.</param>
    #/ <returns>The CandleStickLayer created.</returns>
    function addCandleStick($upColor, $downColor) {
        $this->addOHLCLabel($upColor, $downColor, true);
        $ret = $this->m_mainChart->addCandleStickLayer($this->m_highData, $this->m_lowData,
            $this->m_openData, $this->m_closeData, $upColor, $downColor);
        $ret->setHTMLImageMap("", "", $this->getHLOCToolTipFormat());
        if (count($this->m_highData) - $this->m_extraPoints > 60) {
            $ret->setDataGap(0);
        }

        if (count($this->m_highData) > $this->m_extraPoints) {
            $expectedWidth = ($this->m_totalWidth - $this->m_leftMargin - $this->m_rightMargin) / (
                count($this->m_highData) - $this->m_extraPoints);
            if ($expectedWidth <= 5) {
                $ret->setDataWidth($expectedWidth + 1 - $expectedWidth % 2);
            }
        }

        return $ret;
    }

    #/ <summary>
    #/ Add a HLOC layer to the main chart.
    #/ </summary>
    #/ <param name="upColor">The color of the HLOC symbol for an up day.</param>
    #/ <param name="downColor">The color of the HLOC symbol for a down day.</param>
    #/ <returns>The HLOCLayer created.</returns>
    function addHLOC($upColor, $downColor) {
        $this->addOHLCLabel($upColor, $downColor, false);
        $ret = $this->m_mainChart->addHLOCLayer($this->m_highData, $this->m_lowData,
            $this->m_openData, $this->m_closeData);
        $ret->setColorMethod(HLOCUpDown, $upColor, $downColor);
        $ret->setHTMLImageMap("", "", $this->getHLOCToolTipFormat());
        $ret->setDataGap(0);
        return $ret;
    }

    function addOHLCLabel($upColor, $downColor, $candleStickMode) {
        $i = $this->lastIndex($this->m_closeData);
        if ($i >= 0) {
            $openValue = NoValue;
            $closeValue = NoValue;
            $highValue = NoValue;
            $lowValue = NoValue;

            if ($i < count($this->m_openData)) {
                $openValue = $this->m_openData[$i];
            }
            if ($i < count($this->m_closeData)) {
                $closeValue = $this->m_closeData[$i];
            }
            if ($i < count($this->m_highData)) {
                $highValue = $this->m_highData[$i];
            }
            if ($i < count($this->m_lowData)) {
                $lowValue = $this->m_lowData[$i];
            }

            $openLabel = "";
            $closeLabel = "";
            $highLabel = "";
            $lowLabel = "";
            $delim = "";
            if ($openValue != NoValue) {
                $openLabel = sprintf("Op:%s", $this->formatValue($openValue, $this->m_generalFormat)
                    );
                $delim = ", ";
            }
            if ($highValue != NoValue) {
                $highLabel = sprintf("%sHi:%s", $delim, $this->formatValue($highValue,
                    $this->m_generalFormat));
                $delim = ", ";
            }
            if ($lowValue != NoValue) {
                $lowLabel = sprintf("%sLo:%s", $delim, $this->formatValue($lowValue,
                    $this->m_generalFormat));
                $delim = ", ";
            }
            if ($closeValue != NoValue) {
                $closeLabel = sprintf("%sCl:%s", $delim, $this->formatValue($closeValue,
                    $this->m_generalFormat));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -