如何在iOS中的UIWebView上添加Highcharts?

2024-01-11

我对 HighCharts 完全陌生,我参考了示例http://blog.li-labs.com/developing-ios-apps-with-custom-charting/ http://blog.li-labs.com/developing-ios-apps-with-custom-charting/并尝试了同样的操作,但在 iPad 模拟器上我得到的是空白屏幕。

我已经提到了有关堆栈溢出的问题,但它仍然不起作用。 谁能告诉我哪里错了。

编辑:我的 HTML 文件中的代码:

 <!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
$(function () {

        // Radialize the colors
        Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function(color) {
            return {
                radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 },
                stops: [
                    [0, color],
                    [1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
                ]
            };
        });

        // Build the chart
        $('#container').highcharts({
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: 'Browser market shares at a specific website, 2010'
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage}%</b>',
                percentageDecimals: 1
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        formatter: function() {
                            return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                        }
                    }
                }
            },
            series: [{
                type: 'pie',
                name: 'Browser share',
                data: [
                    ['Firefox',   45.0],
                    ['IE',       26.8],
                    {
                        name: 'Chrome',
                        y: 12.8,
                        sliced: true,
                        selected: true
                    },
                    ['Safari',    8.5],
                    ['Opera',     6.2],
                    ['Others',   0.7]
                ]
            }]
        });
    });


        </script>
    </head>
    <body>
<!--<script src="../../js/highcharts.js"></script>-->
<!--<script src="../../js/modules/exporting.js"></script>-->

<script type="text/javascript" src="highcharts.js"></script>
<script type="text/javascript" src="exporting.js"></script>

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
    </body>
</html>

如何在 XCode 中添加和使用 Javascript 库

下面是我如何在 XCode 中处理代码和数据的简单视图。

  1. 从您喜欢的图表站点下载 Javascript 报告包。就我而言:http://www.highcharts.com/download http://www.highcharts.com/download

  2. 将文件添加或导入到 XCode 资源中。 (我建议创建一个名为 JS 的组)。

  3. 当向 XCode 添加资源时,它通常会被标记为需要编译。确保文件未列为 正在“编译”。最简单的方法是将文件从 目标 -> 你的项目名称 -> 将源代码编译到目标 -> 你的 项目名称 -> 复制捆绑资源。

  4. 创建一个测试 html 文件,或者直接从 HighChart(或任何其他 javascript 图表库)示例文件夹中导入一个文件。就我而言 将其命名为 hcpie.html

  5. 确保标签中的任何文件引用不会递归或移动到任何文件夹中(即使它们位于项目中的一个文件夹中)。 例如

RIGHT =

WRONG =

  1. 在 Interface Designer 中创建一个 UIWebView 并将其链接到您的视图(在我的例子中,我将其命名为 Chart1)。例如

@interface FirstViewController : UIViewController { IBOutlet UIWebView *图表1; }

@end

  1. 加载时只需引用 HTML 示例文件即可。

    • (void)viewDidLoad {

NSString *pathOfFile = [[NSBundle mainBundle] pathForResource:@”hcpie” ofType:@”html”]; NSString *htmlText = [NSString stringWithContentsOfFile:pathOfFile 编码:NSUTF8StringEncoding 错误:无];

NSURL *baseURL = [NSURL fileURLWithPath:pathOfFile];

[chart1 loadHTMLString: htmlText baseURL:baseURL];

[超级viewDidLoad];

}


Steps:-

1)首先创建一个 html 文件并执行所需的代码,或者为了试用可以从包中获得的示例中复制 html 代码http://www.highcharts.com/download http://www.highcharts.com/download

2) 确保 html 中的脚本应该有正确的 .js 链接 例如:-<script src="http://code.highcharts.com/highcharts.js"></script>

or

如果你想在本地提供,你可以写<script src="highcharts.js"></script>但请确保 .js 文件位于您的应用程序文件夹中。

3) NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"graph" ofType:@"html"];
   NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
   [obj loadHTMLString:htmlString baseURL:nil];  // Webview *obj;

希望这对您有帮助。就我而言,它起作用了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在iOS中的UIWebView上添加Highcharts? 的相关文章

随机推荐