5

我对 HighCharts 完全陌生,我参考了示例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

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

  3. 当向 XCode 添加资源时,它通常会被标记为需要编译。确保文件未列为“已编译”。最简单的方法是简单地将文件从 Targets -> Your Project Name -> Compiled Sources 拖入 Targets -> Your Project Name -> Copy Bundle Resources。

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

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

对 =

错误 =

  1. 在界面设计器中创建一个 UIWebView 并将其链接到您的视图(在我的例子中,我将其命名为 chart1)。例如

@interface FirstViewController : UIViewController { IBOutlet UIWebView *chart1; }

@结尾

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

    • (void)viewDidLoad {

NSString *pathOfFile = [[NSBundle mainBundle] pathForResource:@”hcpie” ofType:@”html”]; NSString *htmlText = [NSString stringWithContentsOfFile:pathOfFile encoding:NSUTF8StringEncoding error:nil];

NSURL *baseURL = [NSURL fileURLWithPath:pathOfFile];

[chart1 loadHTMLString: htmlText baseURL:baseURL];

[超级视图DidLoad];

}

4

3 回答 3

6

脚步:-

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

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

或者

如果您想在本地提供它,您可以编写<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;

希望这对您有所帮助。在我的情况下,它的工作。

于 2013-05-02T07:27:18.117 回答
1

我尝试了这篇文章中的建议,但无法加载 HighChart。

问题最终是“loadHTMLString”调用。为 baseURL 传递 nil 是不正确的。这对我有用 - 我需要传递实际的 baseURL:

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:url ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];

NSURL *baseURL = [NSURL fileURLWithPath:htmlFile];

[webView loadHTMLString:htmlString baseURL:baseURL];
于 2013-12-03T20:56:02.810 回答
0

在 UIWebView 中调用 .html 的有用代码。

-(void)viewDidLoad
   {
     NSString *pathOfFile = [[NSBundle mainBundle]  pathForResource:@"chart" ofType:@"html"]; 

     NSString *htmlText =[NSString stringWithContentsOfFile:pathOfFile encoding:NSUTF8StringEncoding error:nil];

     NSURL *baseURL = [NSURL fileURLWithPath:pathOfFile];

    [UIWebView loadHTMLString: htmlText baseURL:baseURL];
   }
于 2015-08-31T11:09:42.663 回答